Why is using the move constructor in a return statement legal?

后端 未结 2 721
天命终不由人
天命终不由人 2021-01-13 01:42

Consider the following:

#include 

#define trace(name) std::cout << #name << \" (\" << this << \"), i = \" << i         


        
2条回答
  •  自闭症患者
    2021-01-13 02:10

    C++11 12.8/32: When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.

    Returning a local automatic variable meets the criteria for elision; treating it as if it were an rvalue selects the move constructor.

提交回复
热议问题