C++ 17: Mandatory Copy Elision

Copy elision is a performance improvement in C++ 17, that prevents temporary objects to be copied when they don’t have to be. Such as passing or returning them by value. To demonstrate how powerful it is:


class arg_class
{
public:
	arg_class()
	{
		cout << "()" << endl;
	}

	void do_something()
	{
		cout << "..." << endl;
	}

	~arg_class()
	{
		cout << "~()" << endl;
	}
};

void foo(arg_class a)
{
	a.do_something();
}

Here is an output for foo(arg_class()); call:

()
...
~()

As you can see, arg_class is only created once! Benefit is obviously increased performance.


To contact me, send an email anytime or leave a comment below.