Exception and Copy Constructor : C++

前端 未结 2 2028
无人共我
无人共我 2021-01-11 16:12

Referring to http://en.wikipedia.org/wiki/Copy_elision

I run below code:

#include 

struct C {
  C() {}
  C(const C&) { std::cout         


        
2条回答
  •  不要未来只要你来
    2021-01-11 16:16

    throw c;     
    

    Creates a temporary object and it is this temporary object that is thrown. The creation of the temporary might be through copy/move constructor. And yes this copy/move can be elided.


    References:
    C++11 15.1 Throwing an exception

    §3:

    A throw-expression initializes a temporary object, called the exception object, the type of which is determined by removing any top-level cv-qualifiers from the static type of the operand of throw and adjusting the type.........

    §5:

    When the thrown object is a class object, the copy/move constructor and the destructor shall be accessible, even if the copy/move operation is elided (12.8).

提交回复
热议问题