int &&在模板实例化中是否有效?

hu *_*ang 3 c++ templates

请考虑以下代码.那里发生了什么?

#include <iostream>
#include <stdio.h>

using namespace std;

template<class T>
void test(T &t)  // but why  int&& is valid here,when  instantiated with test<int&>??
{
  puts("T&");
}

int tref(int&& param)    //compile  error
{
  puts("tref&");
}

int main()
{
  int i;
  test<int&>(i);
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*eas 7

我不知道我跟着,如果你的意思是int&&在第一个,因为Tint&你错了.当Tint&时,T&在签名仍然是int&.另外,int&&没有做与构成int&一个额外的&.&&具有特定含义:rvalue-reference.

§8.3.2[dcl.ref]/5

不应引用引用,不引用引用数组,也不引用引用指针.[...]

§8.3.2[dcl.ref]/6

如果typedef(7.1.3),类型模板参数(14.3.1)或decltype-specifier(7.1.6.2)表示类型TR是对类型T的引用,则尝试创建类型"对cv TR的左值引用"创建对T"的类型"左值引用",而尝试创建类型"对cv TR的rvalue引用"则创建类型TR.