c++11 - Question about r-value in C++0x -


Ravelus IMHO C ++ is significantly improved, but at the beginning 'it looks quite please see the code given below: / P>

  #include & lt; String & gt; Std :: String & amp; Amp; Foo (zero) {std :: string message ("hello!"); Return std :: step (message); } Zero times (const std :: string and message2) {if (message2 == "Bye Bye!") Returns; } Int main () {bar (foo ()); }  

The last owner of the message2 original message object returned by foo () No?

No, it is not right that you fall for the general trap when searching for an attitude reference Found in the accepted answer to the fat! (And I also made this mistake, in my last test with RFT)

If you do not want to shoot in feet while dealing with RFT, then internalize it:

In most cases, a rvalue referenced in a function returns a general reference as a function of stupidity in returning.

Because rvalue references are ... references! So if you return a reference - RAWWUL or "message", then you return the context of the object that was just destroyed, because it was out of the scope, so you return a false reference. This is a bug.

Also, do not return std :: move (message) because the compiler is already aware that "message" is a temporary (a rvalue), so there is no need to rearrange it. With std :: move and actually, writing back std :: move (some) may optimize (at least MSWC 10 disables this RVO)

So, the correct and most effective way is:

  #include & lt; String & gt; Std :: string foo (zero) {std :: string message ("hello!"); Return message; } Zero times (const std :: string and message2) {if (message2 == "Bye Bye!") Returns; } Int main () {bar (foo ()); }  

Good old C ++ 03 :) Because there is no copy in the NVVO and no transcript, there is no step, there is only one construct.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -