下面的小例子说明了我的问题:
template<class T> struct X
{
static void xxx(T& x) { }
static void xxx(T&& x) { }
};
int main(int argc, char** argv)
{
int x = 9;
X<int>::xxx(x); // OK.
X<int&>::xxx(x); // ERROR!
return 0;
}
错误信息(海湾合作委员会):
error: ‘static void X::xxx(T&&) [with T = int&]’ cannot be overloaded
error: with ‘static void X::xxx(T&) [with T = int&]’
为什么? T = int&
---> static void xxx(T& x)
中的T&
是否被替换为int&&
?
如果问题的答案是肯定的,那么:
T&
不是左值引用,而是右值引用!- 下面的代码应该可以工作:
但它没有:
template<class T> struct X
{
static void xxx(T& x) { }
};
int main(int argc, char** argv)
{
X<int&>::xxx(2); // ERROR!
return 0;
}
错误信息(海湾合作委员会):
error: no matching function for call to ‘X::xxx(int)’
note: candidates are: static void X::xxx(T&) [with T = int&]
然后 T&
和 T = int&
不等于 T&&
并且不是右值引用。但如果不是,为什么第一个例子不起作用? (这是一个递归问题!)
但是指针类型没有出现类似的问题:
#include <iostream>
template<class T> struct X
{
static void xxx(T* x) { std::cout << **x << std::endl; }
};
int main(int argc, char** argv)
{
int x = 10;
int* xx = &x;
X<int*>::xxx(&xx); // OK. call X<int*>::xxx(int**)
return 0;
}
为什么引用在此行为中不同?
最佳答案
C++11 语言标准在 §8.3.2[dcl.ref]/6 中解释了它是如何工作的(为了便于阅读而重新格式化):
If a typedef, a type template-parameter, or a decltype-specifier denotes a type
TR
that is a reference to a typeT
,
- an attempt to create the type "lvalue reference to cv
TR
" creates the type "lvalue reference toT
"- an attempt to create the type "rvalue reference to cv
TR
" creates the typeTR
.
让我们考虑一下您的示例(我已将您的 T
重命名为 TR
因此它与上面的语言匹配):
template<class TR> struct X
{
static void xxx(TR& x) { }
static void xxx(TR&& x) { }
};
如果我们尝试实例化 X
与 TR = int&
(所以,T = int
),xxx
的实例化如下:
static void xxx(TR& x) { } --> static void xxx(int& x) { }
static void xxx(TR&& x) { } --> static void xxx(int& x) { }
在第一种情况下,我们尝试创建一个“对 TR
的左值引用”,它变成了“对 T
的左值引用”。 T
是int
, 所以参数类型变为 int&
.
在第二种情况下,我们尝试创建一个“对 TR
的右值引用”,它变为 TR
,即 int&
.
两个重载的参数类型相同,因此出现错误。
关于c++ - 模板参数的引用变量的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3542497/