When should RVO kick-in?

前端 未结 4 1811
我在风中等你
我在风中等你 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:08

    The correct answer is "whenever the compiler pleases". Such behavior is not mandated (but is allowed) by the standard, and the exact conditions where it kicks in varies from compiler to compiler and version to version.

    As a general rule, the compiler is smarter than you, and working in your best interest. Do not question it.

    rvalue references in C++0x are sort of a manual version of RVO.

    Edit: Looking closer at your code, you're definately misunderstanding RVO. Because your parameter is a reference, there is no way the return value of the function could have the same address.

提交回复
热议问题