Wednesday, January 30, 2019

c++ - Is the default Move constructor defined as noexcept?



It seems that a vector will check if the move constructor is labeled as noexcept before deciding on whether to move or copy elements when reallocating. Is the default move constructor defined as noexcept? I saw the following documentation but it didn't specify this.http://en.cppreference.com/w/cpp/language/move_constructor




Implicitly-declared move constructor




If no user-defined move
constructors are provided for a class type (struct, class, or union),
and all of the following is true: there are no user-declared copy
constructors there are no user-declared copy assignment operators
there are no user-declared move assignment operators there are no
user-declared destructors the implicitly-declared move constructor is
not defined as deleted due to conditions detailed in the next section
then the compiler will declare a move constructor as an inline public
member of its class with the signature T::T(T&&) A class can have
multiple move constructors, e.g. both T::T(const T&&) and T::T(T&&).

If some user-defined move constructors are present, the user may still
force the generation of the implicitly declared move constructor with
the keyword default.



Answer



I think the answer is 15.4/14 (Exception specifications):




An inheriting constructor (12.9) and an implicitly declared special member function (Clause 12) have an exception-specification. If f is an inheriting constructor or an implicitly declared default constructor, copy constructor, move constructor, destructor, copy assignment operator, or move assignment operator, its implicit exception-specification specifies the type-id T if and only if T is allowed by the exception-specification
of a function directly invoked by f’s implicit definition; f allows all exceptions if any function it directly invokes allows all exceptions, and f has the exception-specification noexcept(true) if every function it directly invokes allows no exceptions.





Basically, it Does What You Think, and the implicitly-declared move constructor is noexcept whenever it can be.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...