Does const-correctness give the compiler more room for optimization?

[Edit: OK so this question is more subtle than I thought at first.] Declaring a pointer-to-const or reference-of-const never helps any compiler to optimize anything. (Although see the Update at the bottom of this answer.) The const declaration only indicates how an identifier will be used within the scope of its declaration; it does not … Read more

Sell me on const correctness

This is the definitive article on “const correctness”: https://isocpp.org/wiki/faq/const-correctness. In a nutshell, using const is good practice because… It protects you from accidentally changing variables that aren’t intended be changed, It protects you from making accidental variable assignments, and The compiler can optimize it. For instance, you are protected from if( x = y ) … Read more