Questions tagged [copy-elision]

Copy elision refers to an exception to the as-if rule allowing to omit copies

Copy Elision is an exception to the as-if rule governing the behavior of C++ programs.

Return-value-optimization is copy-ellision applied to value-returns.

12.8 Copying and moving class objects [class.copy]

31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.123
This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

  • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value
  • in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object (15.1) can be omitted by constructing the automatic object directly into the exception object
  • when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move
  • when the exception-declaration of an exception handler (Clause 15) declares an object of the same type (except for cv-qualification) as the exception object (15.1), the copy operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.
272 questions
0
votes
0 answers

When move semantics required on practice (in case there is copy elision)

I've run some example programs and noticed that move functions (constructor and assignment operator) never used until i compile with disabled copy elision (in GCC "-fno-elide-constructors"). So the questions are: When in practice (with modern…
morte
  • 325
  • 1
  • 10
0
votes
0 answers

move semantics and copy-elision

I know has been asked several times, I read several tutorial about it but still I can't figure out an answer. Let's have the typical class class Foo { public: Foo() : i_(0) { std::cout << "Foo Empty ctor" << std::endl; } …
Moia
  • 2,216
  • 1
  • 12
  • 34
0
votes
1 answer

Interaction between std::move, return value optimization and destructors

There are many threads on Stack Overflow about the interaction between std::move and copy elision, e.g. What are copy elision and return value optimization? How to be confident of copy elision / return-value optimization c++11 Return value…
cero
  • 158
  • 7
0
votes
1 answer

C++ copy on member access

I was doing some experiments to see when copy is performed apart from copy elision, RVO, NRVO cases. So I've written some code like this: class X { public: X() { std::cout << "Default constructor" << std::endl; } X(const X&) { std::cout <<…
0
votes
1 answer

Can copy elision be perfomed in aggregate initialization in c++17?

Given: //C++17 #include struct Foo { int i; std::string str; }; int main() { Foo foo{1, std::string("Hello, world!")}; } Can Foo::i and Foo::str be directly initialized from 1 and std::string(...) instead of being copied into…
JiaHao Xu
  • 2,452
  • 16
  • 31
0
votes
3 answers

Is there any way to achieve move elision

When running the following code: struct Copy { Copy() {std::cerr << __PRETTY_FUNCTION__ << std::endl;} Copy(const Copy & other) {std::cerr << __PRETTY_FUNCTION__ << std::endl;} Copy & operator=(const Copy & other) {std::cerr <<…
ioquatix
  • 1,411
  • 17
  • 32
0
votes
0 answers

function returning object is not performing deep copy. Why temp returned by foo() not deep copied?

#include using namespace std; class A { int k; int *ptr; public: A(int a):k(a) { cout<<"ctor\n"; ptr = new int[k]; for(int i = 0; i < k; i++) { *(ptr + i) = i*i*i; } } A(const A &obj) { cout<<"copy…
Sadik Khan
  • 29
  • 3
0
votes
2 answers

Const reference to extend object's life followed by const_cast, is it a good idea?

Consider the following function that returns a big object: std::list DoSomething() { std::list TheList; //do stuff return TheList; } I want to get the list, not a copy of it. Because if I have to copy such…
0
votes
1 answer

Copy elision seems to occur even if compiling with -fno-elide-constructors

#include class A { public: A() { std::cout << "Constructor" << std::endl; } A(const A& a) { std::cout << "Copy Constructor" << std::endl; } A& operator=(const A& a) { std::cout << "Copy = operator" << std::endl; } A(A&&…
cuv
  • 1,159
  • 2
  • 10
  • 20
0
votes
2 answers

Why is copy elision so limited?

The two forms of copy elision I care about are very restricted. It's only allowed in a return statement and when initializing a variable with a temporary. So these don't involve copy elision: // Not initialization Noisy b; b = Noisy{}; // Not…
0
votes
1 answer

Two-step copy elision to capture rvalue in constructor call as instance variable

I am trying to get an rvalue instance of this class: #include #define msg(x) std::cout << x " constructor\n" struct X { int i; X(int i) : i(i) {msg("X");} X(const X& x) : i(x.i) {std::cout << "X copy\n";} X(X&& x)…
Stefan
  • 4,380
  • 2
  • 30
  • 33
0
votes
1 answer

Copy elision and return value optimization versus copy constructor

I've been reading about how Copy ellision and return value optimization can improve speed by avoiding calls to an object copy constructor. I understand how the mechanisms work, but I wondered if this could not lead to programs that don't behave as…
Kian
  • 1,654
  • 1
  • 14
  • 22
0
votes
1 answer

Should the return value of binary operator+ overload be const and can it interfere with optimizations?

Given the example code: class Integer { int i_; public: Integer(int i) : i_(i) {} const Integer operator+(const Integer &arg) const { return Integer(i_ + arg.i_); } }; I started wondering whether operator+() should actually return a…
neuviemeporte
  • 6,310
  • 10
  • 49
  • 78
-1
votes
1 answer

How is mandatory copy elision implemented so that the omitted copy/move constructor is not checked for accessibility?

This question may not be useful in practice, but I would really like to understand how the compiler sees the code behind the scenes, so I asked... Assume that I am using C++17 or newer. Consider the following code: class X { public: X() {} …
CPPL
  • 726
  • 1
  • 10
-1
votes
1 answer

Return value optimization with std::pair

I am currently quite puzzled with C++17`s guaranteed RVO and its implications. I understand that for NRVO to kick in, I need to make sure that to return one and the same instance of an object through all possible return path of the function and…
CD86
  • 979
  • 10
  • 27
1 2 3
18
19