c++ - 模板参数的引用变量的问题

标签 c++ templates c++11 rvalue-reference lvalue

下面的小例子说明了我的问题:

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 type T,

  • an attempt to create the type "lvalue reference to cv TR" creates the type "lvalue reference to T"
  • an attempt to create the type "rvalue reference to cv TR" creates the type TR.

让我们考虑一下您的示例(我已将您的 T 重命名为 TR 因此它与上面的语言匹配):

template<class TR> struct X
{
    static void xxx(TR& x)  { }
    static void xxx(TR&& x) { }
};

如果我们尝试实例化 XTR = 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 的左值引用”。 Tint , 所以参数类型变为 int& .

在第二种情况下,我们尝试创建一个“对 TR 的右值引用”,它变为 TR ,即 int& .

两个重载的参数类型相同,因此出现错误。

关于c++ - 模板参数的引用变量的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3542497/

相关文章:

c++解决指针和整数之间的比较

c++ - 为什么 gcc 允许使用大于数组的字符串文字初始化 char 数组?

android - 使用 qt-android 获取通知

c++ - 模板类中模板函数的困难

c++ - boost spirit 的帮助函数(具有模板化返回类型的模板)

c++ - 添加 #include <boost/asio.hpp> 导致 "has different size"问题

c++ - 对模板基类成员函数的 undefined reference

c++ - 将函数指针作为非类型模板参数传递

c++ - C++11 中的数据竞争、UB 和计数器

c++11 - 在具有复制和移动向量和复制赋值的 C++ 类中 : no need for move assignment?