When is a good time to return rvalue links?

Question

After reading tons of articles about rvalue links, I know that:

std::string&& f_wrong() { std::string s("hello"); return std::move(s); } 

wrong and:

 std::string f_right() { std::string s("hello"); return s; } 

sufficient to invoke the std::string move constructor (or any movable constructive classes). In addition, if the return value is to be used to build the object, the named return value optimization (NRVO) is applied, the object will be created directly at the target address, so the move constructor will not be called:

 std::string s = f_right(); 

My question is: when is a good time to return rvalue links? As far as I could think, it seems that returning an rvalue link does not make sense, except for functions like std::move() and std::forward() . It's true?

Thanks!

References

C ++ 0x: do people understand rvalue references? | Pizer weblog

return link rvalue

C ++ 11 rvalues ​​and moving semantics (return statement)

Is returning an rvalue reference more efficient?

+6
source share

Source: https://habr.com/ru/post/984406/


All Articles