Well, itβs not so, whether the compiler can use RVO, but whether it can thereby avoid creating a copy.
Consider:
struct Blah { int x; Blah( int const _x ): x( _x ) { cout << "Hum de dum " << x << endl; } }; Blah foo() { Blah const a( 1 ); if( fermatWasRight() ) { return Blah( 2 ); } return a; }
Getting side effects (output from the constructor) right here, at first glance, is quite incompatible with building a
directy in the repository provided by the caller. But if the compiler is smart enough, then he may notice that the destruction of this object is zero. And more generally, for a specific situation, if the compiler is smart enough, it may be able to avoid the copy operation, no matter how difficult we are developing the code.
Iβm not sure of the formality, although, but with a higher load on the object, so copying will be more expensive, this is one case where the semantics of movement can help, so that optimization will be guaranteed regardless of whether the compiler (or not).
source share