#
When certain criteria are met, an implementation is allowed to omit the creation of a class object from a source object of the same type (ignoring cv-qualification), even if the selected constructor and/or the destructor for the object have side effects.
In such cases, the implementation treats the source and target of the omitted initialization as simply two different ways of referring to the same object.
If the first parameter of the selected constructor is an rvalue reference to the object's type, the destruction of that object occurs when the target would have been destroyed; otherwise, the destruction occurs at the later of the times when the two objects would have been destroyed without the optimization.
[Note 1: 
Because only one object is destroyed instead of two, and the creation of one object is omitted, there is still one object destroyed for each one constructed.
— end note]
This elision of object creation, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):
Copy elision is not permitted where an expression is evaluated in a context requiring a constant expression ([expr.const]) and in constant initialization ([basic.start.static]).
[Note 3: 
It is possible that copy elision is performed if the same expression is evaluated in another context.
— end note]
[Example 1: 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; / well-formed, a.p points to a constexpr A b = g(); / error: b.p would be dangling ([expr.const]) void h() { A c = g(); / well-formed, c.p can point to c or be dangling }
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 non-local object t2.
Effectively, the construction of t can be viewed as directly initializing 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]
[Example 2: class Thing { public: Thing(); ~Thing(); Thing(Thing&&); private: Thing(const Thing&); }; Thing f(bool b) { Thing t; if (b) throw t; / OK, Thing(Thing&&) used (or elided) to throw t return t; / OK, Thing(Thing&&) used (or elided) to return t } Thing t2 = f(false); / OK, no extra copy/move performed, t2 constructed by call to f struct Weird { Weird(); Weird(Weird&); }; Weird g(bool b) { static Weird w1; Weird w2; if (b) return w1; / OK, uses Weird(Weird&) else return w2; / error: w2 in this context is an xvalue } int& h(bool b, int i) { static int s; if (b) return s; / OK else return i; / error: i is an xvalue } decltype(auto) h2(Thing t) { return t; / OK, t is an xvalue and h2's return type is Thing } decltype(auto) h3(Thing t) { return (t); / OK, (t) is an xvalue and h3's return type is Thing&& } — end example]
[Example 3: template<class T> void g(const T&); template<class T> void f() { T x; try { T y; try { g(x); } catch (.) { if (/*...*/) throw x; / does not move throw y; / moves } g(y); } catch(.) { g(x); g(y); / error: y is not in scope } } — end example]

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant