In another question I ran across this code:
Real StatData::mean(Real trim) const { // trim, pun not intended const_cast<StatData&>(*this).items.sort(); // trim } cppreference also has an example on their page:
struct type { type() :i(3) {} void m1(int v) const { // this->i = v; // compile error: this is a pointer to const const_cast<type*>(this)->i = v; // OK } int i; }; Aside from the obvious question of why this would ever be practical, is it unsafe? Does it matter of the object created is const or not and more importantly, is the this pointer safe from undefined behavior because it's only marked const for that one function?
itemsis and whatsortdoes...const StatData x; x.mean(Real());>>> ???? >>> NOT PROFIT