The following code shows that if a template taking a ref-to-const
parameter is instantiated with a reference type (e.g., int&
), the parameter isn't const
:
#include <iostream>
template<typename T>
void f(const T& arg) // arg isn't const if T is a reference type
{
arg = -1;
}
int main()
{
int x = 0;
f<int&>(x); // instantiate f with reference type
std::cout << x << '\n'; // prints -1 under gcc, clang, and msvc
}
What's going on here?
My guess is that the initial type of arg
is int & const &
and that this somehow transforms to int&
. If that's so, exactly how does that happen, in terms of the standard? If that's not what's going on, what is?