const_cast
does work the way you think it does. However, auto
doesn't do what you think it does. auto
works similarly to function template parameter deduction (in fact, it's defined in terms of the latter). Now consider:
template<typename T>
void f(T x);
f(42);
int const n = 42;
f(n);
Both calls are to f<int>()
, not f<const int>()
. Top-level const modifiers are ignored by template parameter deduction. For the same reason, in this example
auto a = 42; a = 84;
auto b = n; b = 84;
variables a
and b
are of type int
, not const int
, and can be modified.