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
520
votes
5 answers

What are copy elision and return value optimization?

What is copy elision? What is (named) return value optimization? What do they imply? In what situations can they occur? What are limitations? If you were referenced to this question, you're probably looking for the introduction. For a technical…
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
119
votes
2 answers

How does guaranteed copy elision work?

At the 2016 Oulu ISO C++ Standards meeting, a proposal called Guaranteed copy elision through simplified value categories was voted into C++17 by the standards committee. How exactly does guaranteed copy elision work? Does it cover some cases where…
jotik
  • 17,044
  • 13
  • 58
  • 123
74
votes
2 answers

Return value optimization and copy elision in C

Some people are not aware that it's possible to pass and return structs by value in C. My question is about the compiler making unnecessary copies when returning structs in C. Do C compilers such as GCC use Return value optimization(RVO)…
Z boson
  • 32,619
  • 11
  • 123
  • 226
69
votes
3 answers

Exact moment of "return" in a C++-function

It seems like a silly question, but is the exact moment at which return xxx; is "executed" in a function unambiguously defined? Please see the following example to see what I mean (here live): #include #include #include…
ead
  • 32,758
  • 6
  • 90
  • 153
34
votes
2 answers

What is copy elision and how does it optimize the copy-and-swap idiom?

I was reading Copy and Swap. I tried reading some links on Copy Elision but could not figure out properly what it meant. Can somebody please explain what this optimization is, and especially what is mean by the following text This is not just a…
Yogesh Arora
  • 2,216
  • 2
  • 25
  • 35
32
votes
4 answers

Correctly propagating a `decltype(auto)` variable from a function

(This is a follow-up from "Are there any realistic use cases for `decltype(auto)` variables?") Consider the following scenario - I want to pass a function f to another function invoke_log_return which will: Invoke f; Print something to…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
27
votes
3 answers

Can I copy-elide an immovable & uncopyable function result into an optional?

I want to store a non-trivial type that is immovable and not copyable in an std::optional. However the object is constructed by a free function. (example) struct Foo { Foo(); Foo(Foo const&) = delete; Foo(Foo&&) = delete; Foo&…
bitmask
  • 32,434
  • 14
  • 99
  • 159
26
votes
3 answers

How to enforce copy elision, why it won't work with deleted copy constructor?

I have an uncopiable class. Copying this would be problematic. I want to guarantee that it won't be ever copied, so I made its copy constructor deleted: class A { public: A(); A(const A&) = delete; }; A fun() { return A(); }; int…
peterh
  • 11,875
  • 18
  • 85
  • 108
25
votes
2 answers

Why is RVO disallowed when returning a parameter?

It's stated in [C++11: 12.8/31] : This elision of copy/move operations, called copy elision, is permitted [...] : — in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object…
Valentin Milea
  • 3,186
  • 3
  • 28
  • 29
25
votes
2 answers

Does copy elision work with structured bindings

Does mandatory copy elision apply to decomposition via structured bindings? Which of the following cases does that apply to? // one auto [one, two] = std::array{SomeClass{1}, SomeClass{2}}; // two auto [one, two] =…
Curious
  • 20,870
  • 8
  • 61
  • 146
25
votes
4 answers

Clang and GCC vs MSVC and ICC: Is a static_assert in the copy/move constructor required to work, if copy/move elision could apply too?

I have a static_assert in a move constructor of a template struct of mine. Is this static_assert required to be considered by the compiler, even if copy elision is possible? This is the stripped-down scenario: #include…
Rumburak
  • 3,416
  • 16
  • 27
25
votes
4 answers

Copy elision for pass-by-value arguments

Given struct Range{ Range(double from, double to) : from(from), to(to) {} double from; double to; }; struct Box{ Box(Range x, Range y) : x(x), y(y) {} Range x; Range y; }; suppose we run Box…
Museful
  • 6,711
  • 5
  • 42
  • 68
23
votes
2 answers

MSVC cannot return an object that can be copied but cannot be moved

While fiddling around with copy elision I came across this strange behavior: class Obj { public: Obj() = default; Obj(Obj&&) = delete; Obj(const Obj&) { std::cout << "Copy" << std::endl; } }; Obj f1() { Obj o; return o; // error C2280:…
Devon Cornwall
  • 957
  • 1
  • 7
  • 17
22
votes
3 answers

Is a copy constructor required when returning by implicit conversion?

The following code compiles fine in Visual C++ 2013, but not under GCC or Clang. Which is correct? Is an accessible copy constructor required when returning an object via an implicit conversion? class Noncopyable { Noncopyable(Noncopyable const…
user541686
  • 205,094
  • 128
  • 528
  • 886
21
votes
3 answers

Why isn't move constructor elided whenever possible with `make_x()` functions?

I cannot figure out why in the last case is the move constructor called when copy elision is enabled (or even mandatory such as in C++17): class X { public: X(int i) { std::clog << "converting\n"; } X(const X &) { std::clog << "copy\n"; } …
1
2 3
18 19