nrvo

C++NRVO guarantees? Or better prefer non-const ref param or shared_ptr?

 ̄綄美尐妖づ 提交于 2019-12-06 07:08:22
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):

Will RVO happen when returning std::pair?

扶醉桌前 提交于 2019-12-05 04:52:01
A function needs to return two values to the caller. What is the best way to implement? Option 1: pair<U,V> myfunc() { ... return make_pair(getU(),getV()); } pair<U,V> mypair = myfunc(); Option 1.1: // Same defn U u; V v; tie(u,v) = myfunc(); Option 2: void myfunc(U& u , V& v) { u = getU(); v= getV(); } U u; V v; myfunc(u,v); I know with Option2, there are no copies/moves but it looks ugly. Will there be any copies/moves occur in Option1, 1.1? Lets assume U and V are huge objects supporting both copy/move operations. Q: Is it theoretically possible for any RVO/NRVO optimizations as per the

Can a C++ compiler perform RVO for a const return value?

♀尐吖头ヾ 提交于 2019-11-29 10:21:25
Let's say I have the function #include <string> std::string const foo() { std::string s = "bar"; return s; } int main() { std::string t = foo(); } Can a compiler perform (named) return-value optimization for t , even though the types of s and t are both different from the return type of foo due to the const -ness difference? (If the answer is different for C++03 and C++11 then I'm definitely interested in knowing the C++03 answer.) There is no way for RVO optimization to break the promise of a const , so there's no problem: RVO can be performed. However, move semantics is affected by the const