Search⌘ K
AI Features

Guaranteed Copy Elision

Explore how guaranteed copy elision in C++17 optimizes object creation by eliminating unnecessary temporary copies. Understand the enhancement from previous standards and see how it benefits your code's clarity and efficiency.

We'll cover the following...

Copy Elision is a common optimisation that avoids creating unnecessary temporary objects.

Copy Elision Example

For example:

C++ 17
#include <iostream>
using namespace std;
struct Test
{
Test() { std::cout << "Test::Test\n"; }
Test(const Test&) { std::cout << "Test(const Test&)\n"; }
Test(Test&&) { std::cout << "Test(Test&&)\n"; }
~Test() { std::cout << "~Test\n"; }
};
Test Create()
{
return Test();
}
int main()
{
auto n = Create();
}

In the above call, you might assume a temporary copy is used - to store the ...