AMDG Ovanes Markarian wrote:
I know that the second snippet is illegal in C++, but your statement was that const is only a part of type system and has no impact on optimization, I just answered you with my doubts, and made a first obvious example with NRVO, which highly depends on const.
This is a case of programmer optimization rather than compiler optimization, though.
const might also involve some optimizations, e.g. in multi-threaded context. Let's imagine a value being passed to a function by const reference. This function starts 2 or more threads and passes the value to them as reference to a const object as well. What happens in this context? As long it is not volatile it might be cached in a register, but I think that if the value is const, compiler has stronger assumption, that this value is good enough to be cached for all threads...
If you have a reference to const, the compiler cannot make any assumptions about whether it is modified or not, because of const_cast. Objects declared const are another matter, because it is undefined behavior to modify them. In Christ, Steven Watanabe