该计划如下:
#include <iostream>
using namespace std;
template <typename F, typename T1, typename T2>
void flip2(F f, T1 &&t1, T2 &&t2)
{
f(t2, t1);
}
void g(int &&i, int &j)
{
cout << i << " " << j << endl;
}
int main(void)
{
int i = 1;
flip2(g, i, 42);
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨:
error: rvalue reference to type 'int' cannot bind to lvalue of type 'int'
Run Code Online (Sandbox Code Playgroud)
但据我所知,正如T2实例化那样int,那么类型t2是int&&,所以应该允许它传递给函数g的第一个参数(int &&). …