c++ - How can I be sure a routine is taking advantage of (N)RVO? -
i'd make sure routines leveraging (n)rvo whenever possible. other parsing through resulting disassembly, there can or check see if routine being compiled (n)rvo? @ point i'm interested in msvc , gcc.
no, not really.
however can follow guidelines when writing code.
unnamed return value optimization
this pretty triggered every time return temporary, in debug mode.
return myobject(....);
named return value optimization
this pretty triggered every time function return same temporary object:
myobject func() { myobject result; if (...) { return result; } result.push(0); return result; }
you can mix those, becomes nigh impossible compiler apply rvo in case:
myobject func() { myobject result; if (...) { return myobject(...); } return result; }
here, 1 return benefit rvo , other not. , bet on first being optimized because you'd stuck if speculatively create result
in return slot , need take if
branch. note reordering statements work:
myobject func() { if (...) { return myobject(...); } myobject result; return result; }
so rule of thumb nrvo there should no return
statement between declaration of result
, return result;
statement return else result
itself.
if follow this, stack odds in favor. , it's matter of code review.
and make code easier read since not declare variables before knowing need them!
Comments
Post a Comment