Parameter elision

suggest change

When you pass an argument to a function, and the argument is a prvalue expression of the function’s parameter type, and this type is not a reference, then the prvalue’s construction can be elided.

void func(std::string str) { ... }

func(std::string("foo"));

This says to create a temporary string, then move it into the function parameter str. Copy elision permits this expression to directly create the object in str, rather than using a temporary+move.

This is a useful optimization for cases where a constructor is declared explicit. For example, we could have written the above as func("foo"), but only because string has an implicit constructor that converts from a const char* to a string. If that constructor was explicit, we would be forced to use a temporary to call the explicit constructor. Copy elision saves us from having to do a needless copy/move.

Feedback about page:

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


Copy elision:
* Parameter 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