3 ms·
> No it doesn't. Parameters are constructed by the caller (in order to allow eliding the parameter copy, if the argument is a temporary anyway), therefore retur
by mavam 11y ago
> No it doesn't. Parameters are constructed by the caller (in order to allow eliding the parameter copy, if the argument is a temporary anyway), therefore returning a parameter cannot be elided because that would have required the caller to know to construct the parameter in the return value slot.
Sorry, I confounded two things here. What I meant to say is that "x" is an xvalue in both scenarios and you wouldn't write std::move(x) in either case. For the overload f(X) you'd always incur a move, whereas for the overload f() you'd always get a copy elision.
Concretely, I've looked at the assembly of this code:
#include <string>
struct X {
std::string str;
};
auto f(X x) -> X {
return x;
}
auto f() -> X {
X x;
return x;
}
auto main() -> int {
X x = f();
auto y = f(x);
}
When compiling with
c++ -g -std=c++14 test.cc && otool -tV a.out | c++filt
the assembly shows that indeed shows that f(X) invokes the move constructor, whereas the copy is elided for f().