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::move
strategically, perhaps on a function call itself likestd::move(MakeType())
, to avoid unnecessary copying? - What should the signature of
param_type
be such that the above assignment tot
is the most efficient?