lundi 26 février 2018

C++11: Why rvalue reference parameter implicitly converted to lvalue

Following is the simplistic code of my problem,

void overloaded (int&& x) {
  cout << "[rvalue]";
}

template <class T>
void fn (T&& x) {
  overloaded(x);
}

int main() {
    fn(0);
    return 0;
}

I got a compile error

cannot bind ‘int’ lvalue to ‘int&&

  overloaded(x);

I am confused here, x is passed as a rvalue reference into fn(). But why the overload() call in fn() complains x is a lvalue?

Aucun commentaire:

Enregistrer un commentaire