Copy elision
In C++ computer programming, copy elision refers to a compiler optimization technique that eliminates unnecessary copying of objects. The C++ language standard generally allows implementations to perform any optimization, provided the resulting program's observable behavior is the same as if, i.e. pretending, the program were executed exactly as mandated by the standard. Beyond that, the standard also describes a few situations where copying can be eliminated even if this would alter the program's behavior, the most common being the return value optimization (see below). Another widely implemented optimization, described in the C++ standard, is when a temporary object of class type is... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
In C++ computer programming, copy elision refers to a compiler optimization technique that eliminates unnecessary copying of objects. The C++ language standard generally allows implementations to perform any optimization, provided the resulting program's observable behavior is the same as if, i.e. pretending, the program were executed exactly as mandated by the standard. Beyond that, the standard also describes a few situations where copying can be eliminated even if this would alter the program's behavior, the most common being the return value optimization (see below). Another widely implemented optimization, described in the C++ standard, is when a temporary object of class type is copied to an object of the same type. As a result, copy-initialization is usually equivalent to direct-initialization in terms of performance, but not in semantics; copy-initialization still requires an accessible copy constructor. The optimization can not be applied to a temporary object that has been bound to a reference. ==Example== import std; // a global mutable variable int n = 0; struct MyStruct { explicit MyStruct(maybe_unused int x) {} MyStruct(maybe_unused const MyStruct& x) { ++n; // the copy constructor has a visible side effect // it modifies an object with static storage duration } }; int main() { MyStruct c1(42); // direct-initialization, calls MyStruct::MyStruct(int) MyStruct c2 = MyStruct(42); // copy-initialization, calls MyStruct::MyStruct(const MyStruct&) std::println("{}", n); // prints 0 if the copy was elided, 1 otherwise } According to the standard a similar optimization may be applied to objects being thrown and caught, but it is unclear whether the optimization applies to both the copy from the thrown object to the exception object, and the copy from the exception object to the object declared in the exception-declaration of the catch clause. It is also unclear whether this optimization only applies to temporary objects, or named objects as well. Given the following source code: import std; struct MyStruct { MyStruct() = default; MyStruct(maybe_unused const MyStruct& x) { std::println("Hello World!"); } }; void f() { MyStruct c; throw c; // copying the named object c into the exception object. // It is unclear whether this copy may be elided (omitted). } int main() { try { f(); } catch (MyStruct c) { // copying the exception object into the temporary in the // exception declaration. // It is also unclear whether this copy may be elided (omitted). } } A conforming compiler should therefore produce a program which prints "Hello World!" twice. In the C++11 revision of the C++ standard, the issues have been addressed, essentially allowing both the copy from the named object to the exception object, and the copy into the object declared in the exception handler to be elided. GCC provides the -fno-elide-constructors option to disable copy-elision. This option is useful to observe (or not observe) the effects of return value optimization or other optimizations where copies are elided. It is generally not recommended to disable this important optimization. C++17 provides for "guaranteed copy elision", a prvalue is not materialized until needed, and then it is constructed directly into the storage of its final destination. == Return value optimization== In the context of the C++ programming language, return value optimization (RVO) is a compiler optimization that involves eliminating the temporary object created to hold a function's return value. RVO is allowed to change the observable behaviour of the resulting program by the C++ standard. import std; struct MyStruct { MyStruct() = default; MyStruct(maybe_unused const MyStruct& x) { std::println("A copy was made."); } }; MyStruct f() { return MyStruct(); } int main() { std::println("Hello World!"); MyStruct obj = f(); } Depending upon the compiler, and that compiler's settings, the resulting program may display any of the following outputs: Hello World! A copy was made. A copy was made. Hello World! A copy was made. Hello World! struct Data { char bytes[16]; }; Data f() { Data result; // generate result return result; } int main() { Data d = f(); } may generate code equivalent to this: struct Data { char bytes[16]; }; Data* f(Data* _hiddenAddress) { Data result; // copy result into hidden object *_hiddenAddress = result; return _hiddenAddress; } int main() { Data _hidden; // create hidden object Data d = *f(&_hidden); // copy the result into d } which causes the Data object to be copied twice. In the early stages of the evolution of C++, the language's inability to efficiently return an object of class type from a function was considered a weakness. Around 1991, Walter Bright implemented a technique to minimize copying, effectively replacing the hidden object and the named object inside the function with the object used for holding the result: struct Data { char bytes[16]; }; void f(Data* p) { // generate result directly in *p } int main() { Data d; f(&d); } Bright implemented this optimization in his Zortech C++ compiler. This particular technique was later coined "Named return value optimization" (NRVO), referring to the fact that the copying of a named object is elided. import std; using std::string; string f(bool cond = false) { string first = "first"; string second = "second"; // the function may return one of two named objects // depending on its argument. RVO might not be applied return cond ? first : second; } int main() { string result = f(); } ==References==
Cite this
"Copy elision". 1782689435. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1361612606 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy