C++ Copy Elision Copy initialization elision

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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.



Got any C++ Question?