Search code examples
c++gccg++copy-elision

GCC NRVO/RVO warning


Is there any warning, which allows us to know whether NRVO/RVO performed or not, in GCC?

I found that -fno-elide-constructors turns off NRVO/RVO, but NRVO/RVO has its own conditions to occur and sometimes does not occur. There is a need to know if NRVO/RVO occurs to understand, when extra copy-construction happens.

I am especially interested in compile-time features. It would be nice if there were some specific #pragma GCC... (which activates the diagnostic immediately following itself) or something using static assertion mechanism.


Solution

  • Is there any warning, which allows us to know whether NRVO/RVO performed or not, in GCC?

    gcc (v14 and up) has -Wnrvo

    "Warn if the compiler does not elide the copy from a local variable to the return value of a function in a context where it is allowed by [class.copy.elision]. This elision is commonly known as the Named Return Value Optimization."

    gcc v14.1 is available on https://godbolt.org/