0

If I understand correctly copy elision will happen if an object is returned like this:

CSomeObject getObject(){

    //....
    //....
    return CSomeObject(...);
}

Is it safe to assume that copy elision will also happen here:

CSomeObject getObject(){

    CSomeObject some_object;
    some_object.setStuff();
    some_object.setMoreStuff();
    //....
    //....
    return some_object;
}

CSomeObject some_object = getObject();

There is only one return statement and always the same temporary object is returned. What about when the return is delegated / indirect:

CSomeObject getObject_2(){
    //....
    return getObject();
}

CSomeObject some_object = getObject_2();
AdyAdy
  • 988
  • 6
  • 19

1 Answers1

1

"C++ Will copy elision happen when returning a temporary object" - it may. It's not guaranteed until C++17 (and even there, certain conditions apply). But, most compilers will elide the copy.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70