Understanding return value optimization and returning temporaries - C++

后端 未结 4 1603
再見小時候
再見小時候 2020-11-28 07:26

Please consider the three functions.

std::string get_a_string()
{
    return \"hello\";
}

std::string get_a_string1()
{
    return std::string(\"hello\");
}         


        
4条回答
  •  臣服心动
    2020-11-28 07:37

    In two first cases RVO optimization will take place. RVO is old feature and most compilers supports it. The last case is so called NRVO (Named RVO). That's relatively new feature of C++. Standard allows, but doesn't require implementation of NRVO (as well as RVO), but some compilers supports it.

    You could read more about RVO in Item 20 of Scott Meyers book More Effective C++. 35 New Ways to Improve Your Programs and Designs.

    Here is a good article about NRVO in Visual C++ 2005.

提交回复
热议问题