Shachar Shemesh
Shachar Shemesh

Reputation: 8563

rvalue reference argument turning into an lvalue

Please consider the following code:

class a {
public:
    int a;
};

void func1( a &&var1 );

void func2( a &&var2 )
{
    func1(var2);
}

When trying to compile it, gcc returns the following:

question.cpp: In function ‘void func2(a&&)’:
question.cpp:10:14: error: cannot bind ‘a’ lvalue to ‘a&&’
     func1(var);
              ^
question.cpp:6:6: error:   initializing argument 1 of ‘void func1(a&&)’
 void func1( a &&var );
      ^

It seems that var2 is an lvalue, despite it being quite explicitly defined as an rvalue reference. Does the double ampersands lose their meaning once assigned? What is the mechanism that's at work here?

Upvotes: 3

Views: 463

Answers (3)

Cubbi
Cubbi

Reputation: 47418

There is no mechanism at work, just the definitions. var2 is an identifier that denotes a variable. When used as an expression, it becomes id-expression, whose value category is defined as follows, in 5.1.1[expr.prim.general]/8

The result is the entity denoted by the identifier. The result is an lvalue if the entity is a function, variable, or data member and a prvalue otherwise.

var2, the variable, has type rvalue reference to a

var2, the expression, has type a and value category lvalue

(you're not the first person to confuse rvalues and rvalue references)

Upvotes: 4

Rubens
Rubens

Reputation: 14768

Inside the function implementation, the once-rvalue becomes a regular variable (l-value), to which you can make assignments. If you want to turn it into an rvalue again, you must use std::move, as in:

void func2( a &&var2 ) {
    func1(std::move(var2));
}

Upvotes: 5

Alex Shtoff
Alex Shtoff

Reputation: 2640

In func2 you could, theoretically, write a = <something>. Therefore a is an l-value. So the compiler is correct.

Upvotes: 2

Related Questions