Copy elision in C++
The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach is used by almost every modern compiler.
What is the purpose of copy elision ?
In order to initialise an object, there are locations in the standards where an object is replicated or transferred. Copy elision (also known as return value optimization (RVO)) is a continuous improvement in which a compiler is allowed to skip a copy or move even if the standard requires it.
Copy elision is a C++ compiler rule that allows it to disregard the temporary's creation and subsequent copy/destruction. That is, the compiler can use the temporary's initialising expression to directly initialise the function's return value. This definitely saves time and effort. It does, however, have two noticeable impacts on the user:
- The copy/move constructor that would have been invoked must be present in the type. Even if the copy/move is omitted by the compiler, the type must be capable of being copied/moved.
- In situations where elision is possible, the side effects of copy/move constructors are really not guaranteed.
Example of Copy Elision :
#include <iostream>
using namespace std;
class classNames
{
public:
classNames(const char *strng = "\0")
{
cout << " Default Constructor! " << endl;
}
classNames(const classNames &x)
{
cout << "Copy constructor! " << endl;
}
};
int main()
{
classNames objectNames = "copy me";
return 0;
}
Output :
Default Constructor!
Explanation :
In the above example, the programme generated a Default Constructor. This is because, when an object of the class classNames was created, one parameter constructor was used to turn the text "copy me" into a temporary object, which was then transferred to the object objectNames.
Example – 2 :
#include <iostream>
using namespace std;
class Abc
{
public:
Abc(const char* strng = "\0") //default constructor
{
cout << " Default Constructor is called!" << endl;
}
Abc(const Abc &aobj) //copy constructor
{
cout << "Copy constructor is called!" << endl;
}
};
int main()
{
Abc aobj1 = "copy me"; // Creating an object of class Abc
return 0;
}
Output :
Default Constructor is called!
How to Reduce the Overhead ?
Modern compilers are frequently optimised to decrease overhead. This is done by trying to break down the copy initialization statement.
classNames objectNames = "copy me";
is divided into
classNames objectNames("copy me");
They are considered to be a better approach in C++. This kind of overheads are generally avoided by C++ compilers.
If we still want to make sure the compiler doesn't encapsulate the call to copy constructor [disable copy elision], we may build the programme with the "-fno-elide-constructors" option and get the following output:
[email protected]:~$ g++ copy_elision.cpp -fno-elide-constructors
[email protected]:~$ ./a.out
Default constructor!
Copy constructor!
If the "-fno-elide-constructors" option is used, the default constructor is used to build a temporary object, followed by the copy constructor, which copies the temporary object to objectNames.
Return Value Elision :
If a function returns a prvalue expression, and the prvalue expression is of the similar type as of the function's return type, the copy from the prvalue temporary can be skipped.
Syntax :
std::string funct()
{
return std::string("foo");
}
In this instance, almost all compilers will omit the temporary structure.
Parameter elision :
When you send an argument to a function that is a prvalue expression of the method's parameter type that isn't a reference, the building of the prvalue can be skipped.
Syntax :
void func(std::string strng) { ... }
func(std::string("foo"));
This instructs you to make a temporary string and then place it in the strng function parameter. Instead of utilising a temporary+move, copy elision allows this statement to build the object in strng directly.
Named return value elision :
If a function returns a lvalue expression, and this lvalue:
- denotes a function-specific automated variable that will be removed after the return
- The automated variable is not really a function parameter,
- and the variable's type is the same as the return type of the function.
When all of these conditions are met, the lvalue copy/move can be skipped.
Syntax :
std::string funct()
{
std::string strng("foo");
//Do stuff
return strng;
}
Conclusion :
In this article we got to know about the copy elision in C++, the purpose of using the copy elision in C++ and even saw an example to show how it works.