Non-movable/Non-copyable type
Explore how C++17's mandatory copy elision allows working with non-movable and non-copyable types by constructing objects in place. Understand the impact on performance, portability, and the return-by-value pattern, preparing you for deeper concepts like updated value categories.
We'll cover the following...
With C++17 we get clear rules on when elision has to happen, and thus constructors might be entirely omitted. In fact, instead of eliding the copies the compiler defers the “materialisation” of an object.
Why might this be useful?
- To allow returning objects that are not movable/copyable - because we could now skip copy/move constructors
- To improve code portability since every conformant compiler supports the same rule
- To support the “return by value” pattern rather than using output arguments
- To improve performance
Let’s look at an Example
Below you can see an example with a non-movable/non-copyable type, based on P0135R0:
The above code wouldn’t compile under C++14 as it lacks copy and move constructors. But
with C++17 the constructors are not required - because the object largeNonMovableObj will be
constructed in place.
Please notice that you can also use many return statements in one function and copy elision will
still work.
Moreover, it’s important to remember, that in C++17 copy elision works only for unnamed temporary objects, and Named RVO is not mandatory.
To understand how mandatory copy elision/deferred temporary materialisation is defined in the C++ Standard, we must understand value categories which are covered in the next section.
In the next lesson, we’ll take a look at the new updated value categories introduced in C++ 17. Read on to find out more.