[class.copy.elision] (original) (raw)

:

class Thing { public: Thing(); ~Thing(); Thing(const Thing&); };

Thing f() { Thing t; return t; }

Thing t2 = f();

struct A { void *p; constexpr A(): p(this) {} };

constexpr A g() { A loc; return loc; }

constexpr A a;
constexpr A b = g();

void h() { A c = g();
}

Here the criteria for elision can eliminate the copying of the object t with automatic storage duration into the result object for the function call f(), which is the global object t2.

Effectively, the construction of the local object tcan be viewed as directly initializing the global object t2, and that object's destruction will occur at program exit.

Adding a move constructor to Thing has the same effect, but it is the move construction from the object with automatic storage duration to t2 that is elided.

end example