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
1
vote
1 answer

I can't find a quote in [class.copy.elision] confirming that the initialization `T x = T();` is eligible for the mandatory copy elision

cppreference.com cites two cases for the Mandatory elision of copy/move operations. I'm interested in the second case as follows: In the initialization of an object, when the initializer expression is a prvalue of the same class type (ignoring…
Alexander
  • 2,581
  • 11
  • 17
1
vote
2 answers

Returning variable created inside the loop causes destructor to be called twice

I am trying to understand what does the C++ standard say about how/when the destructor should be called when an object is returned from the function - Consider this simple struct and two functions - #include int g = 0; struct foo { …
Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
1
vote
2 answers

G++ 7.1.0 onwards supports guaranteed copy elision in this constructor, but Clang++ 4.0 onwards do not

I've been trying to produce a class with a member that is an std::array of a non-copyable type, that I need to initialize in the constructor. I had thought, referring to answers on this SO question, that the following would work: #include…
jwimberley
  • 1,696
  • 11
  • 24
1
vote
1 answer

C++ optimization: avoid a copy operation

I have the following: #include #include class T { public: T() {std::cout << "Default constructor called" << std::endl;} T(const T&) {std::cout << "Copy constructor called" << std::endl;} }; static T s_t; T foo() { …
1
vote
2 answers

How can I initialize non-static private template member variables from temporaries in-place, i.e., without making a copy or move?

I'd like to initialize two non-static private template member variables of a class template from a temporary in-place, i.e., without making a copy or move. For clarification, consider the following example code: #include struct P { …
plexando
  • 1,151
  • 6
  • 22
1
vote
1 answer

additional copy in std::transform when not setting lambda return type

I was struggling with the following code sample with gcc 7.3 and c++17: https://wandbox.org/permlink/UT3RR9jgRmr3VBWv #include #include #include #include struct Y { Y ( int const & s ) : y(s) { std::cout…
nnolte
  • 1,628
  • 11
  • 25
1
vote
1 answer

Will RVO work in this case?

I'm not sure if I should bother at all because it's not any safety critical application but I'm just curious and can't figure it out myself: will the compiler apply RVO for the following method? QJsonObject MyClass::buildObject(const QVector2D&…
tomdol
  • 381
  • 3
  • 14
1
vote
1 answer

How to have "factory function" return a non copyable object?

Context Trying to create some gzip archive with a different filename inside I wrote this following snippet of code. #include #include #include #include…
matovitch
  • 1,264
  • 11
  • 26
1
vote
1 answer

Why there is no temporary object when returning an object through the constructor?

I'm trying to figure out what exactly happens when returning an object through the constructor(conversion function). Stonewt::Stonewt(const Stonewt & obj1) { cout << "Copy constructor shows up." << endl; } Stonewt::Stonewt(double lbs) { …
Guodong Hu
  • 303
  • 1
  • 2
  • 11
1
vote
1 answer

Compiler Optimisation in the context of copy elision

My reference is to the code segment presented as part of the follow-up discussion on the below write-up: Lambdas vs. Closures The pertinent code segment is struct PrintD { ~PrintD() { cout << "dtor" << endl; } }; int main() { PrintD d; auto f =…
user9196120
  • 381
  • 3
  • 9
1
vote
2 answers

Copy elision of map iterator pair value

In the following MVE, does the return value of the Get function qualify for copy elision? EDIT I've change the example somewhat. With Visual Studio 2017 in both Debug and Release builds I see a copy construction on the return statement. Hopefully…
tuskcode
  • 317
  • 2
  • 14
1
vote
2 answers

copy elision of temporary object

CPP Refs states: — 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…
1
vote
1 answer

copy elision using STL (vector as example)

I was reading about the copy elision in c++. And i was having doubts about STL in c++ using this copy elision. The following code: #include #include using namespace std; vector merge(vector &arrA, vector &arrB) { …
Miguel
  • 21
  • 4
1
vote
2 answers

copy elision and virtual cloning

How to avoid unnecessary copying in the following scenario? Class A contains base-type pointer to big object. class A{ BigBaseClass *ptr; A(const BigBaseClass& ob); ~A(){delete ptr;} }; Sometimes I will need object ob to be copied. So I…
Dmitry J
  • 867
  • 7
  • 20
1
vote
0 answers

How to be confident of copy elision / return-value optimization

I often return objects by value, assuming that copy elision / RVO will happen. But I don't have a good under-the-hood understanding of compilers, and the possibility that sometimes something in my code might sabotage copy elision without my…
Museful
  • 6,711
  • 5
  • 42
  • 68