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

前端 未结 4 789
余生分开走
余生分开走 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:38

    As juanchopanza said, the compiler cannot (at C++ level) violate the "as-if" rule; that is all transformations should produce a semantically equivalent code.

    However, beyond the C++ level, when the code is optimized, further opportunities may arise.

    As such, it really depends on the objects themselves: if the move-constructors/destructors have side effects, and (de)allocating memory is a side effect, then the optimization cannot occur. If you use only PODs, with default move-constructors/destructors, then it will probably be automatically optimized.

提交回复
热议问题