once you get into a mindset of using const keyword like that it just becomes natural no matter how non or complex the method is.
Yes. Yes.
In this example, the notion that the const-qualification may somehow 'allow the compiler to perform some extra optimisations' was true in some prehistoric era. If the entire context of use of an object is visible to the compiler (ie. the const-qualified object is not aliased, is not at namespace scope with external linkage, is not a parameter of a non-inline, non-template function with external linkage, etc.), it knows everything about what is going on with that object. It is a lot better at static-flow analysis than most programmers; and the "as-if" rule allows it to rewrite the code.
http://en.cppreference.com/w/cpp/language/as_if
C++11 deprecated the use of the keyword register
- compilers know what would be optimally placed in a register better than programmers; for about twenty years or so, they have been just ignoring the register
keyword as an optimisation hint. Just as they have been ignoring inline
as an optimisation hint.
const
in a purely local context is for the programmer; it makes no difference to the optimiser.
As an example, with optimisations enabled, this code
namespace
{
unsigned int at_namespace_scope_with_internal_linkage = 0 ;
}
int foobar()
{
at_namespace_scope_with_internal_linkage -= 100 ;
/* const omitted */ auto a = 52 ;
auto b = a / 2 ;
auto c = a + 5 ;
if( b < …