On Sunday, 15 November 2015 at 18:36:20 UTC, Jonathan M Davis wrote:
was originally defined to be const". And in general, _very_ few objects are likely to be constructed as const.

It is common in constructors.

So, in general, in C++, you can cast away const and mutate as much as you'd like.

Well, since it is "shared const", the compiler cannot assume that another thread does not write to the object without whole program analysis. Which leaves the compiler writer with less incentive to do things differently because the object is const locally. Not sure if the FAQ reflects the standard or what current compilers do.

I would expect it to change in the future though, since whole program analysis is becoming more and more realistic. The FAQ also end with this statement:

«Please don’t write saying version X of compiler Y on machine Z lets you change a non-mutable member of a const object. I don’t care — it is illegal according to the language and your code will probably fail on a different compiler or even a different version (an upgrade) of the same compiler. Just say no. Use mutable instead. Write code that is guaranteed to work, not code that doesn’t seem to break.»


Reply via email to