Are there special member functions noexcept or throw ()?

It is clear in the C ++ 11 specification that implicitly generated special functions (i.e. default constructor constructors, destructor, copy / move, and copy / move assignment operators) have exception specifications. But the specification is presented only in terms of legacy dynamic exception specifications (ie, "Throw (T1, T2, T3)"). This is confirmed by the example in 15.4 / 14:

struct A { A(); A(const A&) throw(); A(A&&) throw(); ~A() throw(X); }; struct B { B() throw(); B(const B&) throw(); B(B&&) throw(Y); ~B() throw(Y); }; struct D : public A, public B { // Implicit declaration of D::D(); // Implicit declaration of D::D(const D&) throw(); // Implicit declaration of D::D(D&&) throw(Y); // Implicit declaration of D::D() throw(X, Y); }; 

Notes are not normative, I know, but it should be noted that the constructor of the D-copy is declared throw() instead of noexcept . This matters because the behavior of the program is different if a throw() is violated compared to a noexcept violation.

The text in 15.4 / 14 of the above example is normative, and it says:

An implicitly declared special member function (clause 12) must have an exception specification. If f default constructor, copy constructor, move constructor, destructor, copy assignment operator or redirection operator are implicitly declared, its implicit exception-specification indicates the identifier type T if and only if T is allowed by the exception specification of the function called directly by implicit definition fs; f allows all exceptions if there is a function that it calls directly, allows all exceptions, and f allows no exceptions if every function that it calls directly does not allow an exception.

Given that only dynamic exception specifications are mentioned here, I fear that implicitly created special member functions are never declared noexcept . Is this really so?

+4
source share
1 answer

I do not believe this business. The actual requirement is only that "... f does not allow exceptions if every function that it directly calls does not allow exceptions."

As noted above (Β§15.4 / 12):

The specification does not throw an exception if it has the form throw() , noexcept or noexcept( constant expression ) , where the expression of constants gives true . A function with an exclusive exception specification does not allow any exceptions.

It’s not really a revelation that the specification of exceptions for exclusion of metadata does not allow any exceptions, but I think this suggests that the wording in the description of the specification of an exception other than metadata is almost verbatim repeated in the requirement for an implicitly declared special party function. Thus, it seems to me that any form of exception-free exception specification ( throw() or noexcept or noexcept(<anything that converts to true>) ) is noexcept(<anything that converts to true>) , and that it was specifically intended, and not just an accident of wording.

+1
source

Source: https://habr.com/ru/post/1441698/


All Articles