0

I've always avoided returning objects by value, due to copy-elision being an optimization with observable side-effects... but I've read that C++17 has added a way to ensure returned objects never call their copy constructor and destructor! Is there anything special I need to do in order to trigger this new behavior reliably?

I don't fully understand the rules around it, and it sounds easy to accidentally write a function that triggers the "old" behavior; I want to avoid that at all costs if possible. Generating a compiler error if the compiler lacks this guarantee, (or more realistically, if I mess it up) would grant me peace.

Is there any kind of boilerplate or habit, when coding my class or function, that'd let me absolutely avoid the pre-C++17 behavior?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
  • Related: [Forcing RVO / move construction when returning by value](https://stackoverflow.com/questions/13557410/forcing-rvo-move-construction-when-returning-by-value) – Ranoiaetep Jan 10 '23 at 16:59

0 Answers0