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.
I am not aware of any gcc specific diagnostic message or other method that easily can solve your task. As you have found out,
-fno-elide-constructors
will disable copy/move elisions, so you will know for sure that (N)RVO will not happen in that case at least.However, a quick look at paragraph 31 in section 12.8 of this C++11 working draft states that:
When copy/move elision happen the local auto object is the same as the temporary (return) object, which in turn is the same as the "storage" object (where the return value is stored). So the local auto object is the same as the storage object, which means a pointer comparison will equal true. A simple example to demonstrate this:
Enabling/disabling
-fno-elide-constructors
changes the printed message as expected. Note: in the strictest sense the pointer comparison might be depending on undefined behavior when (N)RVO does not happen, since the local auto object is non-existing.Doing pointer comparisons will add cruft, but with the advantage of compiler-independency.