3

以下のコードを検討してください。そこで何が起こっているのですか?

#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);
}
4

1 に答える 1

7

あなたが間違っているint&&ので、あなたが最初のものを意味しているなら、私がフォローするかどうかはわかりません. の場合、署名の はまだです。また、extraでの作成とは関係ありませんrvalue-referenceという特定の意味があります。Tint&Tint&T&int&int&&int&&&&

§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) が型 T への参照である型 TR を示す場合、型 " 「cv TR への左辺値参照」は「T への左辺値参照」型を作成しますが、「cv TR への右辺値参照」型を作成しようとすると、TR 型が作成されます。

于 2012-08-17T02:39:05.100 に答える