How to enforce copy elision, why it won’t work with deleted copy constructor?

Until C++17 copy elision is an optimization the compiler is not required to do, so classes must be copyable since the compiler might want to copy (even if it actually does not). In C++17 copy elision will be guaranteed in many cases and then classes won’t need copy ctors. See also: http://en.cppreference.com/w/cpp/language/copy_elision http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0135r0.html https://herbsutter.com/2016/06/30/trip-report-summer-iso-c-standards-meeting-oulu/ (the … Read more

Does the behavior of guaranteed copy elision depend on existence of user-defined copy constructor?

Quoting from C++17 Working Draft ยง15.2 Temporary Objects Paragraph 3 (https://timsong-cpp.github.io/cppwp/class.temporary#3): When an object of class type X is passed to or returned from a function, if each copy constructor, move constructor, and destructor of X is either trivial or deleted, and X has at least one non-deleted copy or move constructor, implementations are permitted … Read more

How does guaranteed copy elision work?

Copy elision was permitted to happen under a number of circumstances. However, even if it was permitted, the code still had to be able to work as if the copy were not elided. Namely, there had to be an accessible copy and/or move constructor. Guaranteed copy elision redefines a number of C++ concepts, such that … Read more

What are copy elision and return value optimization?

Introduction For a technical overview – skip to this answer. For common cases where copy elision occurs – skip to this answer. Copy elision is an optimization implemented by most compilers to prevent extra (potentially expensive) copies in certain situations. It makes returning by value or pass-by-value feasible in practice (restrictions apply). It’s the only … Read more