2
votes
#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

1
Yes, it's normal. Reference-collapsing rules would only apply if the constructor itself is a template. - ildjarn
If reference-collapsing rules always applied to rvalue-reference syntax regardless of context then it wouldn't be possible to specifically denote a true rvalue-reference, which wouldn't be very useful. - ildjarn
In void f(int && v), v is always an rvalue-reference. In template<typename U> void f(U && v), v can be an rvalue-reference or an lvalue-reference due to reference-collapsing rules. - ildjarn
This might make it more clear to you. - Xeo
Because you're passing an lvalue, not an rvalue. - ildjarn

1 Answers

2
votes

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.