Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy elision method

Tags:

c++

oop

From the standard definition of copy elision method:

In C++ computer programming, copy elision refers to a compiler optimization technique that eliminates unnecessary copying of objects.

Let us consider following code:

#include <cstdlib>
#include <iostream>
using namespace std;
int n=0;
struct C 
{
 C (int) {}
 C(const C&) {++n;}      



       };
int main(int argc, char *argv[])
{
    C c1(42);
    C c2=42;




return n;
}

This line "return n" will returns either 0 or 1, depending on whether the copy was elided.

Also consider this code:

#include <iostream>

struct C {
  C() {}
  C(const C&) { std::cout << "Hello World!\n"; }
};

void f() {
  C c;
  throw c; // copying the named object c into the exception object.
}          // It is unclear whether this copy may be elided.

int main() {
  try {
    f();
  }
  catch(C c) { 
}
}

It says that

// copying the exception object into the temporary in the exception declaration.
//It is also unclear whether this copy may be elided.

So my question is how useful is implement such optimization method, if sometimes results are undefined? And in general how often it is used?

like image 665
dato datuashvili Avatar asked Sep 30 '11 08:09

dato datuashvili


People also ask

What is copy move elision?

Copy elision is a compiler optimization technique that eliminates unnecessary copying/moving of objects.

Is copy elision guaranteed?

Copy elision (NRVO) is allowed there and is routinely performed by most compilers, but is still non-guaranteed, and the widget class cannot be non-copyable non-movable.

What is elision programming?

It is in this sense that computer language designers have borrowed the term: an elision in a programming language is when the language makes optional what might be thought of as a necessary portion of the grammar. There are a number of legal elisions in C#.

How do I disable copy elision?

GCC provides the -fno-elide-constructors option to disable copy-elision. This option is useful to observe (or not observe) the effects of return value optimization or other optimizations where copies are elided. It is generally not recommended to disable this important optimization.


1 Answers

The important bit is that the standard explicitly allows for this, and that means that you cannot assume that the side effects of a copy constructor will be executed as the copies might be elided. The standard requires that the implementation of a copy-constructor has copy-constructor semantics, that is, has as whole purpose the generation of a second object semantically equivalent in your domain to the original object. If your program complies with that, then the optimization will not affect the program.

It is true, on the other hand, that this is the only situation I can think where the standard allows for different visible outcomes from the same program depending on what the compiler does, but you have been advised that you should not have side effects in your copy constructor (or rather, you cannot depend on the exact number of copies performed).

As to whether it is worth it, yes it is. In many cases copies are quite expensive (I am intentionally ignoring move-constructors in C++11 from the discussion). Consider a function that returns a vector<int>, if the copy is not elided, then another dynamic allocation is required, copy of all of the vector contents and then release of the original block of memory all three operations can be expensive.

Alternatively, you could force users to change their code to create an empty object and pass it by reference, but that will make code harder to read.

like image 118
David Rodríguez - dribeas Avatar answered Sep 21 '22 12:09

David Rodríguez - dribeas