Do C++11 compilers turn local variables into rvalues when they can during code optimization?

前端 未结 4 790
余生分开走
余生分开走 2020-12-05 18:02

Sometimes it\'s wise to split complicated or long expressions into multiple steps, for example (the 2nd version isn\'t more clear, but it\'s just an example):



        
4条回答
  •  一生所求
    2020-12-05 18:44

    The compiler cannot break the "as-if" rule in this case. But you can use std::move to achieve the desired effect:

    object3 a(x);
    object2 b(std::move(a));
    object1 c(std::move(b));
    return c;
    

提交回复
热议问题