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

Why can't g++ (and clang++) avoid string construction here?

If we write: std::string foo(const bool b) { if (b) { return "Hello"; } else { return "World"; } } g++ and clang++ will not call a string constructor, but rather manipulate the return string in-place. On the other…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
4 answers

Move constructor vs Copy elision

Can someone please explain me one thing. From the one side the move constructor was designed to optimize the memory & processor usage by eliminating unnecessary copying an objects BUT from other side almost everywhere the move constructor is going…
Derek81
  • 145
  • 9
2
votes
2 answers

Prevent unnecessary copying between large structs

I have huge structs DataFrom and Data (which have different members in reality). Data is Created from DataFrom. struct DataFrom{ int a = 1; int b = 2; }; static DataFrom dataFrom; struct Data{ int a; int b; }; class…
wair92
  • 446
  • 11
  • 24
2
votes
1 answer

Copy/move elision with a check

Consider this. There is a non-copyable, non-movable class, and there is some predicated defined for it: struct AA { AA(AA const& otehr) = delete; AA(AA && otehr) = delete; AA& operator = (AA const& otehr) = delete; AA&…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
2
votes
2 answers

How does this list-like initialization work?

I know about several types of initialization in C++ and recently learned why list initialization should be preferred. But what about this code? live demo #include #include class A { public: A(int x, std::string y) {…
2
votes
1 answer

Move and copy constructor in C++

#include class studant { public: studant () { std::cout<<"studant"<
2
votes
1 answer

No constructor called?

I'm trying to understand constructors calling with l/r values, so I created the class A below : class A { public : A() { cout << "called default constructor" << endl ; } A(const A&) { cout << "called copy constructor" <<…
Saif Faidi
  • 509
  • 4
  • 15
2
votes
3 answers

C++ copy elision of fields

I am trying to get copy elision to work for fields of the object that is to be returned. Example code: #include struct A { bool x; A(bool x) : x(x) { std::cout << "A constructed" << std::endl; } A(const A &other)…
PEC
  • 593
  • 1
  • 5
  • 16
2
votes
2 answers

Copy elision of arguments as return values

Given the following; will C++17 guarantee copy elision? template Widget frobnicate(Widget w) { // optionally mutate w in some way return w; } Does answer change if Widget implements a move constructor or not? Should I ever…
dalle
  • 18,057
  • 5
  • 57
  • 81
2
votes
1 answer

Is there any error in the way I am trying to create temporrary object

Following is my code: I dont understand why the move constructor does not get invoked. Mystring.h: #ifndef _MYSTRING_H #define _MYSTRING_H #include #include #endif class Mystring{ private: char * str; public: …
2
votes
0 answers

C++ primer 5 edition: passing a temporary shared_ptr to function

On C++ primer 5 edition. chapter 12 Dynamic Memory: Consider the following function that operates on a shared_ptr: // ptr is created and initialized when process is called void process(shared_ptr ptr) { // use ptr } // ptr goes out of scope…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
2
votes
1 answer

Move constructor and move assignment operator vs. copy elision

Related questions: Why this move constructor is not called wtih rvalue temporary? [duplicate] Move Constructor vs Copy Elision. Which one gets called? move Constructor is not called I'm posting this question because this thing of the move…
Enlico
  • 23,259
  • 6
  • 48
  • 102
2
votes
1 answer

Copy constructor elision for direct initialization when the argument is converted to the destination type

This question is about the wording of the c++ standard. All compilers, and I think this is what should be, elide the copy constructor for the initialization of the object b bellow (assembly here): struct B; struct A{ operator B(); }; struct…
Oliv
  • 17,610
  • 1
  • 29
  • 72
2
votes
1 answer

How copy constructor can be called with rvalue

MWE: struct A { A() {std::cout << "constructor" << std::endl; } A(const A& a) {std::cout << "copy constructor" << std::endl; } A(A&& a) {std::cout << "move constructor" << std::endl; } }; int main() { A a1{}; A a2{ a1 }; A…
2
votes
1 answer

Constructing and Destructing of local variable and return variable in C++

#include using namespace std; class A { public: A() { cout << "A()" << endl; } A(const A& a) { cout << "A(const A& a)" << endl; } A(A&& a) { cout << "A(A&& a)" << endl; } A&…
chaosink
  • 1,329
  • 13
  • 27