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
21
votes
2 answers

GCC NRVO/RVO warning

Is there any warning, which allows us to know whether NRVO/RVO performed or not, in GCC? I found that -fno-elide-constructors turns off NRVO/RVO, but NRVO/RVO has its own conditions to occur and sometimes does not occur. There is a need to know if…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
21
votes
2 answers

initializing a non-copyable member (or other object) in-place from a factory function

A class must have a valid copy or move constructor for any of this syntax to be legal: C x = factory(); C y( factory() ); C z{ factory() }; In C++03 it was fairly common to rely on copy elision to prevent the compiler from touching the copy…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
19
votes
3 answers

Copy elision when creating object inside emplace()

I see a lot of code at work where people use emplace and emplace_back with a temporary object, like this: struct A { A::A(int, int); }; vector v; vector.emplace_back(A(1, 2)); I know that the whole point of emplace_back is to be able to…
mystery_doctor
  • 404
  • 2
  • 11
18
votes
1 answer

Does the behavior of guaranteed copy elision depend on existence of user-defined copy constructor?

The following code behaves differently with or without user-defined copy constructor under GCC 8.0.1: #include struct S { int i; int *p; S() : i(0), p(&i) {} // S(const S &s) : i(s.i), p(&i) {} // #1 // S(const S &s)…
xskxzr
  • 12,442
  • 12
  • 37
  • 77
18
votes
2 answers

Guaranteed elision and chained function calls

Let's say I have the following type: struct X { X& operator+=(X const&); friend X operator+(X lhs, X const& rhs) { lhs += rhs; return lhs; } }; And I have the declaration (assume all the named variables are lvalues of…
Barry
  • 286,269
  • 29
  • 621
  • 977
17
votes
3 answers

Why doesn't RVO happen with structured bindings when returning a pair from a function using std::make_pair?

Consider this code, which defines a simple struct Test (with a default constructor and copy constructor) and returns a std::pair from a function. #include #include using namespace std; struct Test { Test() {} …
Telescope
  • 2,068
  • 1
  • 5
  • 22
17
votes
2 answers

Member initialization for non-copyable variable in C++17

When performing member initialization for non-copyable variable (such as std::atomic), it's required to use direct-initialization rather than copy-initialization according to answer here. However when I turn on -std=c++17 in g++ 7.4.0, it seems…
zingdle
  • 491
  • 5
  • 17
17
votes
1 answer

Will 'Guaranteed Copy Elision' (P0135, C++1z) potentially require ABI breakage?

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0135r0.html The above proposal for 'Guaranteed Copy Elision' was voted into the C++ working paper in the June 2016 meeting in Oulu, Finland, which was then voted for publication as a committee…
Phil Miller
  • 36,389
  • 13
  • 67
  • 90
16
votes
2 answers

Constructor initializer list is not calling copy constructor

So I was learning about constructor initializer list and I wrote the following code : class Mango { public: Mango(){cout<<"Mango::ctor()";} Mango(const Mango& other){cout<<"Mango::copy_ctor()";} }; class Box { public: Box() :…
15
votes
6 answers

C++ return value optimization

This code: #include std::vector getstdvec() { std::vector v(4); v[0] = 1; v[1] = 2; v[2] = 3; v[3] = 4; return v; } int main() { std::vector v(4); for (int i = 0; i != 1000; ++i) …
Aurelius
  • 1,146
  • 2
  • 13
  • 25
14
votes
1 answer

What is clang's 'range-loop-analysis' diagnostic about?

Background: Consider the following example: #include #include int main() { std::vector vectorBool{false, true}; for(const auto &element : vectorBool) std::cout << std::boolalpha << element << ' '; return…
Mike
  • 8,055
  • 1
  • 30
  • 44
14
votes
1 answer

Is there any special reason why the move constructor is not elided in the snippet shown below?

gcc, clang and VS2015 don't elide the call to the move constructor in the code below, after throwing object a. It seems to me the conditions established in bullet point (31.2) of §8.12[class.copy]/31 (N4140) are satisfied. #include…
Belloc
  • 6,318
  • 3
  • 22
  • 52
13
votes
3 answers

Can the compiler elide the following copy?

I'm still a rookie programmer, I know that premature optimization is bad, but I also know that copying huge stuff around is bad, as well. I've read up on copy elision and it's synonyms but the examples on Wikipedia for example make it seem to me…
Erius
  • 1,021
  • 8
  • 21
13
votes
1 answer

Copy elision for list-initialization, where is it stated in the standard?

In [dcl.init]/17.6, it is explicitly written that for the case of parenthesis initialization, copy elision occurs: If the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the…
Oliv
  • 17,610
  • 1
  • 29
  • 72
12
votes
2 answers

Unintuitive RVO of function returning non-copyable const value?

Consider the following example code in C++ >=17: struct A{ A() = default; A(const A&) = delete; }; const A f(){ return A{}; } int main(){ const A& a = f(); // OK // A& b = f(); // Error: cannot convert 'const A' to 'A&' …
eivour
  • 1,678
  • 12
  • 20
1
2
3
18 19