For example:
Big create()
{
Big x;
return std::move(x);
// return static_cast<typename std::remove_reference<T>::type&&>(t) // why not elide here?
}
Assuming that applying std::move()
to return a local variable inhibits move-semantics because compilers can't make any assumptions about the inner-workings of functions in general, what about cases when those assumptions are not necessary, for example when:
std::move(x)
is inlined (probably always)std::move(x)
is written as:static_cast<typename std::remove_reference<T>::type&&>(t)
According to the current Standard, an implementation is allowed to apply NRVO...
— 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 (18.3)) 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
Obviously, neither 1) nor 2) qualify. Apart from the fact that using std::move()
to return a local variable is redundant, why is this restriction necessary?