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
3
votes
1 answer

Copy-elision in direct initialization from braced-init-list

In the following program the object A a is directly initialized from braced-init-list {A{}}: #include struct A { int v = 0; A() {} A(const A &) : v(1) {} }; int main() { A a({A{}}); std::cout << a.v; } MSVC and GCC…
Fedor
  • 17,146
  • 13
  • 40
  • 131
3
votes
3 answers

Demonstrating Move Constructor Usefulness

I am trying to demonstrate the usefulness of move constructors in eliminating unnecessary copying. However, when I run in Release, the visual studio optimizer elids the copy. No copy constructor is called when a move constructor is not available,…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
3
votes
0 answers

Copy/move elision in case of delegating to a copy/move constructor

In the following program, the constructor A::A(int) delegates the call to copy/move constructor, which both are explicitly deleted: struct A; A f(); struct A { A() {} A(int) : A(f()) {} A(const A&) = delete; A(A&&) =…
Fedor
  • 17,146
  • 13
  • 40
  • 131
3
votes
1 answer

What has changed in C++17 in terms of MOVE elision

Is the "move elision" guaranteed in C++17? Let me explain what I mean by that. In almost every article on what C++17 has introduced, one can find the term: "guaranteed copy elision for RVO" which is kinda self-explanatory. But what about move…
bielu000
  • 1,826
  • 3
  • 21
  • 45
3
votes
1 answer

Copy elision and trivially copyable types

From the standard 6.7.7 (temporary objects), we can see: When an object of class type X is passed to or returned from a function, if X has at least one eligible copy or move constructor ([special]), each such constructor is trivial, and the…
Antoine Morrier
  • 3,930
  • 16
  • 37
3
votes
1 answer

Why converting constructor needs copy constructor to be declared when explicit cast performed?

From what I learned, I thought Foo a = 1 is equivalent to Foo a = (Foo)1. With copy constructor declared, yes, they both result in calling Converting constructor. class Foo { public: // Converting constructor Foo(int n) : value_(n) {…
Jeongmin Heo
  • 61
  • 1
  • 7
3
votes
1 answer

copy elision in c++03

Copy-elision is, in some cases, mandatory in c++17, and permitted in c++11/14. This in particular concerns copy initialization. For example, the following program #include struct A { explicit A(int){ std::cout << "conversion" <<…
francesco
  • 7,189
  • 7
  • 22
  • 49
3
votes
1 answer

Avoiding temporary construction when returning by value and passing by const reference

I have a function which accepts a Large by const reference: void func(const Large& param); and a class which holds a Large: class HoldsLarge { public: Large GetByValue() const { return l; }; private: Large l; } If I do HoldsLarge…
segfault
  • 339
  • 2
  • 8
3
votes
1 answer

Is copy initialization identical to copy initialization of a copy?

The following two code segments presents the task of initializing variable b as a copy of a. The first code segment initializes the variable using copy initialization (initialization using =). Assume class Apple is simply defined as an empty…
3
votes
2 answers

What is the rationale behind copy elision in C++?

What is the reason the C++ standard allows(requires) compilers to optimize away calls to copy constructor(in certain cases), even though it might contain observable side effects? If I'm not mistaken, the "as if" rule already allows compilers to…
wolfofuniverse
  • 441
  • 4
  • 8
3
votes
3 answers

How is memory managed during C++ copy elision?

#include using namespace std; class A { public : A() { cout<<"constructor is called"<
New_Bee
  • 81
  • 4
3
votes
0 answers

Confusing behaviour around mandatory elision of copy/move

While toying with RVO I encountered the following issue and I can't get my head around it. #include struct A { A* p = this; }; A func() { return A(); } int main() { A a = A(); std::cout << "address of a: " << &a << " -…
3
votes
1 answer

Copy elision for captured local variables in returned lambda

Is the copy (move) construction in a by-value capture ([x]) (or C++14 move capture [x = std::move(x)]) in a lambda expression (as a return value) possible (or guaranteed) to be elided? auto param_by_value(Widget w) { // manipulating w ... …
Ruifeng Xie
  • 813
  • 4
  • 16
3
votes
1 answer

Can I trust NVCC to optimize away std::pair in return types?

Sometimes, one wants to write a (small) CUDA device-side function which returns two values. In C, you would have that function take two out-parameters, e.g.: __device__ void pair_maker(float x, float &out1, float& out2); but in C++, the idiomatic…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
1 answer

Copy elision with std::mutex

This explanation of copy elision states that Under the following circumstances, the compilers are required to omit the copy and move construction of class objects, even if the copy/move constructor and the destructor have observable…
rozina
  • 4,120
  • 27
  • 49