Why does copy elision make an exception for formal parameters?

前端 未结 2 1810
自闭症患者
自闭症患者 2021-01-24 10:55

Here is a complete program:

#include 
using std::cout;
using std::endl;
using std::move;

int count {0};  // global for monitoring


class Triple         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-24 11:31

    If copy elision is disabled, both cases consist of 1 copy followed by 3 moves. (Any output of 2 for your code would indicate a compiler bug).

    The copy is:

    • initialization of left

    and the moves are:

    • initialization of return value from left
    • initialization of temporary object denoted by a+b from return value
    • initialization of result from a+b

    It would be more illuminating to replace count with an output message saying "in copy constructor" and "in move constructor" . Currently you are not tracking moves at all.

    In the pass-by-reference case , all 3 moves can be elided. In the pass-by-value case, 2 of the moves can be elided. The move which cannot be elided is the move from left to the return value.

    I don't know the rationale for why this move can't be elided. Maybe it'd be difficult for a compiler to do something like A a = foo( A() ); if A() were elidable all the way up to a.

提交回复
热议问题