Hello,
I have a question regarding NRVO:
//------------------------------------------------------------------------
Object returnValueOptimization ()
{
Object object;
// . . .
return object; // copy elision
}
//------------------------------------------------------------------------
int main ()
{
auto returnValue = returnValueOptimization (); // copy elision
return 0;
}When I run the code in debug mode in Xcode there is only one constructor call of the class Object - great!
Now lets assume there is a bug in the code between the first and the last line of the returnValueOptimization () method. It’s a quick fix and the developer did not recognize that this method was written with NRVO in mind. After his fix the code looks like this:
//------------------------------------------------------------------------
Object returnValueOptimization ()
{
Object object;
// . . .
if (!isProjectValid)
return Object ();
// . . .
return object;
}
//------------------------------------------------------------------------
int main ()
{
auto returnValue = returnValueOptimization (); // copy elision
return 0;
}Now I have a constructor and a copy constructor call when isProjectValid == false! When no automated performance tests are written for this code area the chances are high that the problem will occur one day during beta testing (or even worse, the customer will recognize it) when reports come in that a particular area of the software is no longer responsive when lots of data is processed.
So my questions is: is there any chance to check if clang is using NRVO? It would be even better if developers can „tag“ their methods as „NRVO method“ and will get a compiler warning if NRVO is not possible.
Any ideas? Tips? Tricks? What are the exact rules for clang with NRVO?
Thanks!
Erich