c++ - Why is using the move constructor in a return statement legal? -



c++ - Why is using the move constructor in a return statement legal? -

consider following:

#include <iostream> #define trace(name) std::cout << #name << " (" << << "), = " << << std::endl class c { c(c const&); c& operator=(c const&); public: int i; c() : i(42) { trace(ctor); } c(c&& other) : i(other.i) { trace(move); other.i = 0; } c& operator=(c&& other) { trace(asgn); other.i = 0; homecoming *this; } ~c() { trace(dtor); } }; c func1(bool c) { c local; if (c) homecoming local; else homecoming c(); } int main() { c local(func1(true)); homecoming 0; }

both msc , g++ allow return local, , utilize move constructor (as shown output) when doing so. while makes lot of sense me, , think should case, can't find text in standard authorizes it. far can see, argument move constructor must either prvalue (which isn't) or xvalue; in fact lvalue, create homecoming illegal c other = local; in function body (which fail compile).

c++11 12.8/32: when criteria elision of re-create operation met or met save fact source object function parameter, , object copied designated lvalue, overload resolution select constructor re-create first performed as if object designated rvalue.

returning local automatic variable meets criteria elision; treating if rvalue selects move constructor.

c++ c++11

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

java - Parsing XML, skip certain tags -

c# - ASP.NET MVC Sequence contains no matching element -