2 ms·
> It allows more sensible code and compiler optimizations. Unfortunately no, const only tells the compiler optimizer something when applied to variables but no
by account42 4y ago
> It allows more sensible code and compiler optimizations.
Unfortunately no, const only tells the compiler optimizer something when applied to variables but not for reference or pointer types (or member functions, where it just means apply const to the this pointer). It would be nice if there were a stronger co_const that would tell the compiler could assume is never cast away (or at least that the variable is not modified through that pointer/reference even if the const is cast away) but C++ const is not that.
- kllrnohj 4y agoThe const in this example is on the member function so nothing external can cast it away, but the value of it there is really more about contract/behavioral enforcement than optimizations yes.