Search code examples
c++c++11visual-c++-2010rvalue-reference

binding a lvalue to a rvalue reference under MSVS2010


#include<iostream>

template<class T>
struct Foo
{
    T v_;

    Foo(T&& v):v_(std::forward<T>(v))
    {
        std::cout << "building Foo..\n";
    }
};


int main()
{
    int v;
    Foo<int> foo(v);

    std::cin.ignore();
}

visual c++ 2010 output :

error C2664: 'Foo<T>::Foo(T &&)' : cannot convert parameter 1 from 'int' to 'int &&'

Is it normal that I can't bind a lvalue to a rvalue reference ?

EDIT:

same thing for a function :

void f(int && v)
{

}
int v;
f(v); //won't compile

I'm a little bit confused because I think std::forward is usefull to detect if a method is called with a lvalue or a rvalue reference. But it seems that we can't bind any lvalue into rvalue reference... at least without template parameter but I don't understand it very well


Solution

  • It's normal that calling a move constructor requires an r-value, not an l-value, since an l-value could be named after the move constructor runs, and using a moved-from value would evoke unintended behavior.