Will return-value optimization occur in the following example? (Possibly compiler-dependent question. In which case I suppose I'm wondering for "typical" modern compilers like recent clang/gcc.)
Blah factory() {
return Blah();
}
void func() {
Blah blah;
if (condition) {
blah = factory();
blah.DoSomething();
} else {
blah = factory();
blah.DoSomethingElse();
}
blah.DoOneMoreThing();
}
I know I'm not explicitly constructing the object in the same line as calling the factory function, but a smart enough compiler could definitely optimize away the copies in the above example (since the instance blah
has not been touched prior to being set to factory()
).
If RVO will indeed not occur, is there some way to avoid the copies without changing factory
?