My question originates from delving into std::move
in return
statements, such as in the following example:
struct A
{
A() { std::cout << "Constructed " << this << std::endl; }
A(A&&) noexcept { std::cout << "Moved " << this << std::endl; }
};
A nrvo()
{
A local;
return local;
}
A no_nrvo()
{
A local;
return std::move(local);
}
int main()
{
A a1(nrvo());
A a2(no_nrvo());
}
which prints (MSVC, /std:c++17, release)
Constructed 0000000C0BD4F990
Constructed 0000000C0BD4F991
Moved 0000000C0BD4F992
I am interested in the general initialization rules for return statements in functions that return by-value and which rules apply when returning a local variable with std::move
as shown above.
The general case
Regarding return statements you can read
- Evaluates the expression, terminates the current function and returns the result of the expression to the caller after implicit conversion to the function return type. [...]
on cppreference.com.
Amongst others Copy initialization happens
- when returning from a function that returns by value like so
return other;
Coming back to my example, according to my current knowledge - and in contrast to the above-named rule - A a1(nrvo());
is a statement that direct-initializes a1 with the prvalue nrvo()
. So which object exactly is copy-initialized as described at cppreference.com for return statements?
The std::move
case
For this case, I've referred to ipc's answer on Are returned locals automatically xvalues. I want to make sure that the following is correct: std::move(local)
has the type A&&
but no_nrvo()
is declared to return the type A
, so here the
returns the result of the expression to the caller after implicit conversion to the function return type
part should come into play. I think this should be an Lvalue to rvalue conversion:
A glvalue of any non-function, non-array type T can be implicitly converted to a prvalue of the same type. [...] For a class type, this conversion [...] converts the glvalue to a prvalue whose result object is copy-initialized by the glvalue.
To convert from A&&
to A
A
's move constructor is used, which is also why NRVO is disabled here. Are those the rules that apply in this case, and did I understand them correctly? Also, again they say copy-initialized by the glvalue but A a2(no_nrvo());
is a direct initialization. So this also touches on the first case.
You have to be careful with cppreference.com when diving into such nitty-gritty, as it's not an authoritative source.
In this case, none. That's what copy elision is: The copy that would normally happen is skipped. The cppreference (4) clause could be written as "when returning from a function that returns by value, and the copy is not elided", but that's kind of redundant. The standard: [stmt.return] is a lot clearer on the subject.
That's not quite right. NRVO only applies to names of non-volatile objects. However, in
return std::move(local);
, it's notlocal
that is being returned, it's theA&&
that is the result of the call tostd::move()
. This has no name, thus mandatory NRVO does not apply.The
A&&
returned bystd::move()
is decidedly not an Lvalue. It's an xvalue, and thus an rvalue already. There is no Lvalue to rvalue conversion happening here.Not really. Whether a function has to perform copy-initialization of its result as part of a
return
statement is not impacted in any way by how that function is invoked. Similarly, how a function's return argument is used at the callsite is not impacted by the function's definition.In both cases,
an
is direct-initialized by the result of the function. In practice, this means that the compiler will use the same memory location for thean
object as for the return value of the function.In
A a1(nrvo());
, thanks to NRVO, the memory location assigned tolocal
is the same as the function's result value, which happens to bea1
already. Effectively,local
anda1
were the same object all along.In
A a2(no_nrvo())
,local
has its own storage, and the result of the function, akaa2
is move-constructed from it. Effectively,local
is moved intoa2
.