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
9
votes
3 answers

RVO force compilation error on failure

Lots of discussions here about when RVO can be done but not much about when it is actually done. As stated may times, RVO can not be guaranteed according to the Standard but is there a way to guarantee that either RVO optimization succeeds or the…
Patrick Fromberg
  • 1,313
  • 11
  • 37
9
votes
1 answer

Passing by Value and copy elision optimization

I came upon https://web.archive.org/web/20120707045924/cpp-next.com/archive/2009/08/want-speed-pass-by-value/ Author's Advice: Don’t copy your function arguments. Instead, pass them by value and let the compiler do the copying. However, I don't…
newprint
  • 6,936
  • 13
  • 67
  • 109
8
votes
3 answers

Can copy elision occur in catch statements?

Consider an exception class with a copy constructor with side-effects. Can a compiler skip calling the copy constructor here: try { throw ugly_exception(); } catch(ugly_exception) // ignoring the exception, so I'm not naming it { } What about…
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
8
votes
1 answer

Is it possible to intentionally defeat guaranteed copy elision for a particular type?

In C++17 and above, guaranteed copy elision means that it's possible to return non-moveable objects frun a chain of functions, all the way to the ultimate caller: struct NonMoveable { NonMoveable() = default; NonMoveable(NonMoveable&&) =…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
8
votes
3 answers

Why is the move constructor involved here

I have this piece of C++ code: class Args {}; class MyClass { public: MyClass(Args& a) {} MyClass(MyClass &&) = delete; }; int main() { Args a; MyClass c1 = MyClass(a); MyClass c2 = a; MyClass c3(a); return 0; } This does not…
skalkoto
  • 159
  • 5
8
votes
2 answers

Should we write `std::move` in the cases when RVO can not be done?

It is known that std::move should not be applied to the function return values because it can prevent RVO (return value optimization). I am interested in the question what should we do if we certainly know that RVO will not happen. This is what the…
Ashot
  • 10,807
  • 14
  • 66
  • 117
8
votes
2 answers

Does guaranteed copy elision work with function parameters?

If I understood correctly, starting from C++17, this code now requires that no copy will be done: Foo myfunc(void) { return Foo(); } auto foo = myfunc(); // no copy Is it also true for function arguments? Will copies be optimized away in the…
Maël Nison
  • 7,055
  • 7
  • 46
  • 77
8
votes
1 answer

Why does for_each return function by move

I was reading the documentation for std::for_each here http://en.cppreference.com/w/cpp/algorithm/for_each and saw that the return value is std::move(f) Why does the standard enforce moving the input parameter in the return value? Won't it be moved…
Curious
  • 20,870
  • 8
  • 61
  • 146
8
votes
1 answer

Storing a pointer to object returned with NRVO

If I write a factory method that instantiates an object locally and then returns by value, intending to take advantage of NRVO (as per some of the answers here: c++11 Return value optimization or move?), will a pointer/reference to the local object…
Scott Oliver
  • 507
  • 5
  • 19
8
votes
5 answers

Is this code well-defined regardless of copy elision?

Consider this code: #include struct Test { int x; int y; }; Test func(const Test& in) { Test out; out.x=in.y; out.y=in.x; return out; } int main() { Test test{1,2}; std::cout << "x: " << test.x << ", y:…
Ruslan
  • 18,162
  • 8
  • 67
  • 136
7
votes
1 answer

Unexpected memcpy on uncopyable & unmovable type when using co_await

Preamble This is a description of what I am trying to do with the code, skip to the next section to see the actual issue. I want to use coroutines in an embedded system, where I can't afford too many dynamic allocations. Therefore, I am trying the…
Lukas Lang
  • 400
  • 2
  • 11
7
votes
5 answers

passing heavy objects C++0x

I have a function which produces a type of expensive object (containing vectors and a maps of a non fixed size) so I really want to avoid invoking copy c'tors. Until now I have just returned a std::shared_ptr from the method and used that but I…
111111
  • 15,686
  • 6
  • 47
  • 62
7
votes
4 answers

Is it possible to ensure copy elision?

Copy elision is a neat optimization technique and in some cases relying on copy elision can actually be faster than passing around references "by hand". So, let's assume you have identified a critical code path where you rely on the fact that the…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
7
votes
1 answer

Is copy/move elision allowed to make a program using deleted functions well-formed?

Consider the following code: #include struct Thing { Thing(void) {std::cout << __PRETTY_FUNCTION__ << std::endl;} Thing(Thing const &) = delete; Thing(Thing &&) = delete; …
user7860670
  • 35,849
  • 4
  • 58
  • 84
7
votes
2 answers

Return local value from function without triggering copy constructor

I am attempting to delete the copy constructor using the c++ type system to prevent copying an object. struct DeleteCopyConstructor { DeleteCopyConstructor() {}; DeleteCopyConstructor(DeleteCopyConstructor& op2) = delete; …
jcarpenter2
  • 5,312
  • 4
  • 22
  • 49