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
#include <iostream>
int n = 0;
struct C {
explicit C(int) { }
C(const C&) { ++n; } // the copy constructor has a visible side effect
}; // it modifies an object with static storage duration
int main() {
C c1(42); // direct-initialization, calls C::C(int) C c2 = C(42); // copy-initialization, calls C::C(const C&)
std::cout << n << std::endl; // 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:
#include <iostream>
struct C {
C() = default; C(const C&) { std::cout << "Hello World!\n"; } };
void f() {
C 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 (C 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 current revision of the C++ standard (C++11), 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.
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.
Summary
In general, the C++ standard allows a compiler to perform any optimization, provided the resulting executable exhibits the same observable behaviour as if (i.e. pretending) all the requirements of the standard have been fulfilled. This is commonly referred to as the "as-if rule". The term return value optimization refers to a special clause in the C++ standard that goes even further than the "as-if" rule: an implementation may omit a copy operation resulting from a return statement, even if the copy constructor has side effects.
The following example demonstrates a scenario where the implementation may eliminate one or both of the copies being made, even if the copy constructor has a visible side effect (printing text). The first copy that may be eliminated is the one where a nameless temporary C could be copied into the function f's return value. The second copy that may be eliminated is the copy of the temporary object returned by f to obj.
#include <iostream>
struct C {
C() = default; C(const C&) { std::cout << "A copy was made.\n"; } };
C f() {
return C(); }
int main() {
std::cout << "Hello World!\n"; C obj = f(); }