Copy initialization elision

suggest change

If you use a prvalue expression to copy initialize a variable, and that variable has the same type as the prvalue expression, then the copying can be elided.

std::string str = std::string("foo");

Copy initialization effectively transforms this into std::string str("foo"); (there are minor differences).

This also works with return values:

std::string func()
{
  return std::string("foo");
}

std::string str = func();

Without copy elision, this would provoke 2 calls to std::string’s move constructor. Copy elision permits this to call the move constructor 1 or zero times, and most compilers will opt for the latter.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Copy elision:
* Copy initialization elision

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
75 Copy elision
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype