The named return value optimization is enabled by copy elision specified in C++17 in [class.copy.elision]. The relevant part here is [class.copy.elision]/1.1:
When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. […]
- in a
return
statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function parameter or a variable introduced by the exception-declaration of a handler ([except.handle])) with the same type (ignoring cv-qualification) as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function call's return object
[…]
emphasis mine. Thus, compilers are allowed to perform the optimization here. And a quick test would seem to verify that compilers will actually perform this optimization here…
Note that the const
may be problematic nevertheless. If a compiler does not perform copy elision (it's only allowed, not guaranteed to happen here; even in C++17 since the expression in the return
statement is not a prvalue), the const
will generally prevent the object from being moved from instead (normally cannot move from const
object)…
return str
creates an object that is a copy ofstr
, so RVO is possible. It is questionable whether NVRO can be performed, since the returned object needs to be distinct fromstr
. However, depending on what analysis the compiler performs if the function is in the same compilation unit as the caller, it could inline and then eliminate the function call entirely, and replacestd::string txt = foo()
with the notional equivalent ofstd::string txt("bar")
.const
must apply to the scope of the variable to guaranteestr
remains unmodified during the call offoo()
. How does destruction work otherwise?