mardi 26 juillet 2016

passing rvalue raises cannot bind to lvalue

The program is as below:

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

The compiler complains:

error: rvalue reference to type 'int' cannot bind to lvalue of type 'int'

But to my understanding, as T2 is instantiated with int, then the type of t2 is int&&, so it should be allowed to pass to function g's first argument (int &&).

What's wrong with my understanding?

Aucun commentaire:

Enregistrer un commentaire