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
3
votes
1 answer

c++14 return a value by reference to optimize performance

I have a method that ultimately will take a value from an internal api call auto val = api->post(req); //step 1 // the post returns a class of "type json" json api::post(const request& request) { //step 2 // do some job json j = << some…
3
votes
2 answers

Shouldn't guaranteed copy elision apply?

I do not understand the behavior of gcc here, I would expect RVO to apply, but regardless of whether I pass optimization flags and/or I pass -std=c++17, in the second case, the gratuitous pair of braces seems to prevent GCC from eliding the copy. $…
akim
  • 8,255
  • 3
  • 44
  • 60
3
votes
1 answer

Why does copy elision not occur in this case?

Consider this code: #include struct S { S(std::string s) : s_{s} { std::cout << "S( string ) c-tor\n"; } S(S const&) { std::cout << "S( S const& ) c-tor\n"; } S(S&& s) { std::cout << "S&& c-tor\n"; s_ = std::move(s.s_); } …
bladzio
  • 414
  • 3
  • 15
3
votes
1 answer

Why aren't C++ compilers optimizing more string constructions away in pass-by-value scenarios?

(This question is inspired by Nicolai Josuttis' CppCon 2017 talk.) Consider the following source file (for an object, not a complete program): #include class C { std::string s_; public: C(std::string s) : s_(s) { }; void…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
4 answers

RAII and factory design pattern?

Let's say I have Foo class: struct Resource { void block(); void unblock(); }; struct Foo { static Foo create() { Resource resource; resource.block(); return Foo{resource}; } ~Foo() { resource.unblock(); } void f()…
user1244932
  • 7,352
  • 5
  • 46
  • 103
3
votes
1 answer

Structured bindings and mandatory copy elision

If you utilize structured bindings like so auto [a, b, c] = std::make_tuple(1, 10.0, "string object"s); then will the copies from the returned tuple be elided and the objects go straight into a, b and c or will the initializations be move…
Curious
  • 20,870
  • 8
  • 61
  • 146
3
votes
2 answers

understanding copy constructor calls and named return value optimization

I have been looking into this article about NRVO. class RVO { public: RVO(){ printf("I am in constructor\n"); } RVO(const RVO& c_RVO) { printf("I am in copy constructor\n"); } ~RVO(){ printf("I am…
Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34
3
votes
1 answer

How to return std::vector from a function using C++11 move semantics?

I know C++11 has move semantics from this link: Elements of Modern C++ Style But it does not introduce how to return a vector using move semantics. How to do this?
Dean Chen
  • 3,800
  • 8
  • 45
  • 70
3
votes
2 answers

Too many destructors called on template classes (N)RVO optimization

I'm trying to write own Smart Pointers (C++11) and stacks with one problem, that can be explained by next example: #include template class TestTemplateClass { private: T_Type _state; public: TestTemplateClass()…
3
votes
2 answers

Auto and copy elision

What exactly are the rules for copy elision when one is using auto and commits to a specific type ? (see: GotW - Almost always auto). From my understanding, the move/copy constructor is required to be accessible even though it's not generally used.…
3XX0
  • 1,315
  • 1
  • 13
  • 25
3
votes
1 answer

copy elision visible side effect

Consider this code: #include using namespace std; struct Foo { public: int _a{}; Foo(int a) : _a{a} { std::cout << "ctor" << std::endl; } Foo(const Foo &) { std::cout << "copy" << std::endl; …
vsoftco
  • 55,410
  • 12
  • 139
  • 252
3
votes
2 answers

std::vector initialization move/copy constructor of the element

I have this piece of code: #include #include using namespace std; class Foo{ public: Foo() noexcept {cout << "ctor" << endl;} Foo(const Foo&) noexcept {cout << "copy ctor" << endl;} Foo(Foo&&) noexcept {cout <<…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
3
votes
1 answer

List initialization and copy elision

Consider the following example: #include struct A { A(int, char*){}; A(const A&){ printf("copy-ctor\n"); } }; int main() { A x = A(5, nullptr); } According to 8.5.16 (of C++11 standard) the line A x = A(5, nullptr); is…
PowerGamer
  • 2,106
  • 2
  • 17
  • 33
3
votes
1 answer

In what cases can't a modern day compiler apply the NRVO optimization for functions?

In general I would like to know when and why a modern day compiler, say gcc 4.7 and up using c++11, can not apply an NVRO optimization. EDIT: I oversimplified this code mistakenly not returning any local variables. A better example was supplied by…
bjackfly
  • 3,236
  • 2
  • 25
  • 38
3
votes
2 answers

Why will the following code also call the copy constructor?

Why is it that when the g_Fun() executes to the return temp it will call the copy constructor? class CExample { private: int a; public: CExample(int b) { a = b; } CExample(const CExample& C) { a = C.a; cout<<"copy"<
user707549