Since C++17, what is guaranteed when a function returns a prvalue such as return Widget{};?
Correct answer
A. The object is built directly in the caller's storage, with no move at all
13 views
A. The object is built directly in the caller's storage, with no move at all
13 views
C++17 made copy elision mandatory for prvalue initialisation: the temporary is materialised directly in the destination, so no copy or move constructor is invoked. The type therefore need not even have an accessible move constructor for such a return. Named return value optimisation (NRVO) remains an optional optimisation, which is where the older must-be-movable intuition still applies.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.