以下代码表明,如果一个模板带有一个引用类型(例如
这里发生了什么?
我猜测最初
int&
)的 ref-to-const
参数被实例化,那么该参数不是 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
}
这里发生了什么?
我猜测最初
arg
的类型是int & const &
,然后以某种方式转变为int&
。如果是这样,按照标准如何进行转换?如果不是这样,那是什么情况呢?