I found here the following "rule":
[...] auto drops const and volatile qualifiers only if they're at the top or right below an outermost reference [...]
I understand that top-level cv-qualifiers are a description of the variable itself (compared to the description of what it is pointing to or referencing). But when is a cv-qualifier "right below an outermost reference" and why would auto drop it (probably the first question answers the second one as well)?
"cv right below an outermost reference" means that the reference is to a cv-qualified type. For example, take this function:
const int& foo();
The type "right below the outermost reference" is const int
, which means the const
is there as well. In this code:
auto i = foo();
the type of i
is int
, not const int
or const int&
.
Examples of a const
which is not right below an outermost reference are:
const char* bar();
const double* volatile & baz();
Using auto
to take calls of these functions would deduce to type const char*
and const double*
, respectively.