When should RVO kick-in?

前端 未结 4 1849
我在风中等你
我在风中等你 2021-01-02 14:46

From the following code, If RVO has happened, I expect to see the 2 addresses pointing to the same location, however this is not the case (my compiler is MS VC9.0)



        
4条回答
  •  忘掉有多难
    2021-01-02 15:09

    You seem to misunderstand RVO, try this example (actually NRVO):

    std::string foo(const char* const s)
    {
        std::string out(s);
        std::cout << "address: " << (void*)(&out) << std::endl;
        return out;
    }
    
    int main()
    {
       std::string s = foo("abc");
       std::cout << "address: " << (void*)(&s) << std::endl;
    }
    

提交回复
热议问题