I've been investigating how copy elision behaves when it is not directly assigned to an lvalue and perhaps chained or used down the road, but haven't found any concrete answers.
For starters, I understand that NRVO occurs in the following example, and the return value is directly constructed in the destination variable:
Type MakeType() {
Type type;
// ...
return type;
}
Type a = MakeType();
However, let's say we have another function which takes in a Type as a parameter:
Type MakeComplexType(/*some signature*/ param_type) {
Type complex_type = param_type
// ...
return complex_type;
}
And we call this as follows:
Type t = MakeComplexType(MakeType());
- Is it possible to chain copy elision all the way to
t? - If not, can we use
std::movestrategically, perhaps on a function call itself likestd::move(MakeType()), to avoid unnecessary copying? - What should the signature of
param_typebe such that the above assignment totis the most efficient?