On my question, I still know only one article, in which the use of NRVO is at least somehow described. An article from MSDN: Named Return Value Optimization in Visual C ++ 2005 . However, I am confused by the year of writing this article and the question arises: And what is the current state of affairs? After all, the yard is not 2005, but 2016!

This article provides three reasons for NOT to work:

  • Different paths returning different named objects.
  • Multiple return paths (even if the same named object is returned) with EH states introduced.
  • The named object returned is referenced in an inline asm block.

Has anything changed since then?

    1 answer 1

    Since then, only the move-semantics has been added, respectively, there have been more optimal paths for returning from a function by values.

    On the other hand, now the forced std::move() may interfere with the use of NVRO. Then I don’t know what is better: stay move and get a guaranteed move, or remove it, but get the options: NVRO, move or copy.

    Not bad to look into the compiler bug trackers. For example, GCC:

    1. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53637
    2. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58051
    3. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58055

    According to the dynamics (especially links 1 and 3), a sensation arises (NVRO appeared in GCC in 3.1: https://gcc.gnu.org/gcc-3.1/changes.html ) that it’s not possible to roam there.

    In the first bug there is a link to an interesting research, quite new (updated sometime in August 2015, according to the gcc version of the trunk), for checking various compilers in the optimization options for unnecessary copying: http://www.byte-physics.de/cpp -copy-elision .