When will a C++11 compiler make RVO and NRVO outperform move semantics and const reference binding?

后端 未结 3 1944
轮回少年
轮回少年 2020-12-30 04:20

Consider the case when \"whole\" objects with move semantics enabled are returned from functions, as with std::basic_string<>:

std::wstrin         


        
3条回答
  •  我在风中等你
    2020-12-30 04:27

    I'm not sure if this is standardized (as Nicol says, all optimizations are up to the compiler), but I heard STL talk about this and (at least in MSVC), RVO happens before anything else. So if there's a chance to apply RVO, then that'll happen without any action on your part. Second, when you return a temporary, you don't have to write std::move (I think this is actually in the standard), since the return value will implicitly be treated as an rvalue.

    The upshot is: Don't second-guess the compiler, just write the most natural-looking code and it'll give you the best-possible result.

提交回复
热议问题