goodbyeera
goodbyeera

Reputation: 3199

return statement binding rvalue reference to an lvalue?

I've been educated from several sources that in C++11 the return value of a function can be move-constructed from a return statement consisting of a named local variable. For example:

class A {};

A f() {
    A a;
    return a;
}

int main() {
    f();
}

That is, in C++11, the prvalue temporary designated by the function call expression f() is move-constructed from the function's local variable a, instead of copy-constructed as in C++03. Of course, this is all semantically speaking, before any level of optimization is taken, copy elision, NRVO, etc.

My question is, the argument of A's move constructor is of type A&&, which can only bind to a prvalue or xvalue, right? So which specific rule of exception allow its binding to the lvalue a here? Thanks.

Upvotes: 1

Views: 228

Answers (1)

Cubbi
Cubbi

Reputation: 47418

which specific rule of exception allow its binding to the lvalue a here?

That's 12.8[class.copy]/32

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.

Upvotes: 5

Related Questions