I have been using C++ since 1992 (and reading copious amounts about the language), so I know a fair amount about the language, but far from all. My question is about C++11 named return value optimization - what guarantees are there that it will be performed? I tend to prefer to send in non-const parameters (C++97 style) or use shared_ptr (C++11 style), or even used ptr-to-ptr (C style). One reason is that with non-const ref args or shared_ptr, I am guaranteed that NO extra object copies are made.
So my question is (especially for those C++ programmers who do hard real-time or kernel work): What idioms do you prefer? I REALLY hope this question is not downvoted as imprecise, opinion-based or plain stupid - I know it is highly relevant for efficient, modern C++ programming.
In Section 12.8/31, the C++11 standard writes
"When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the copy/move constructor and/or destructor for the object have side effects."
That means your compiler might never use RVO (although most compilers do support it).
Given the above, Scott Meyers' advice from 'Effective Modern C++' (Item 25) is
"Never apply std::move or std::forward to local objects if they would otherwise be eligible for the return value optimization."
The rationale is as follows:
- If you do apply std::move, then the move constructor (which is more expensive than RVO) will be used, even if RVO was a possibility. So you possibly lose some performance there.
- If you don't apply std::move, you leave room for RVO should your compiler support it. If your compiler doesn't support RVO, you'll use the move constructor anyway. So you possibly gain some performance there.
Clang will have -Wpessimizing-move
and -Wredundant-move
warnings for this. See this link.
来源:https://stackoverflow.com/questions/29029629/cnrvo-guarantees-or-better-prefer-non-const-ref-param-or-shared-ptr