Lets say I have two functions A and B.
where function A returns an object X and function B gets object X as an argument. For example.
X A() {
X x;
return x;
}
void B(X x) {
write(x.data, x.size);
}
int main() { B(A()); }
Is this object X constructed only once as a temporary of B using RVO or do I need to use move semantics.
If copy elision is used, the X will only be constructed once.
Even if copy elision is disabled (e.g. passing the
-fno-elide-constructorsto gcc), the move constructors will be used automatically.You can visualize it yourself:
With RVO it prints
With no RVO it prints