Since you requested a standard C ++ quote, here it is:
12.8 copying class objects
fifteen
* When certain criteria are met, implementations are allowed to omit the copy construction of the class object, even if the copy constructor and / or object destructor have side effects. In such cases, the implementation considers the source and purpose of the omitted copy operation as just two different ways of accessing the same object, and the destruction of this object occurs at later times when two objects would be destroyed without optimization .111). This permission for copy operations is permitted in the following cases (which can be combined to eliminate multiple copies):
- in the return statement in a function with the type of the returned class, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the returned type of the function, the copy operation can omit the construction of the automatic object directly into the return value of the function.
- when the object of the temporary class that was not attached to the link (12.2) is copied to the class object with the same cv-unqualified type, the copy operation can be omitted by creating a temporary object directly in the target missing copy *
Example:
class Thing { public: Thing(); หThing(); Thing(const Thing&); }; Thing f() { Thing t; return t; } Thing t2 = f();
Here, the elite criteria can be combined to eliminate two calls to the Thing copy constructor:
copying the local automatic object t to a temporary object for the return value of the function f () and copying this temporary object to object t2. In fact, the construction of a local object t can be considered as a direct initialization of the global object t2, and that the destruction of objects will occur when the program exits.
Alok save
source share