This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 114a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-04-18


294. Can static_cast drop exception specifications?

Section: 7.6.1.9  [expr.static.cast]     Status: NAD     Submitter: Steve Adamczyk     Date: 27 Jun 2001

[Picked up by evolution group at October 2002 meeting.]

Is it okay for a static_cast to drop exception specifications?

    void f() throw(int);
    int main () {
      static_cast<void (*)() throw()>(f);  // Okay?
      void (*p)() throw() = f;  // Error
    }

The fact that a static_cast is defined, more or less, as an initialization suggests that a check ought to be made.

One tricky point: this is another case where the general rule that the reverse of an implicit cast is allowed as a static_cast bites you -- the reverse conversion doesn't drop exception specifications, and so is okay. Perhaps this should be treated like casting away constness.

Mike Miller comments : I don't think that case can arise. According to 14.5 [except.spec],

An exception-specification shall appear only on a function declarator in a function, pointer, reference, or pointer to member declaration or definition.

We strengthened that in issue 87 (voted to DR status in Copenhagen) to

An exception-specification shall appear only on a function declarator for a function type, pointer to function type, reference to function type, or pointer to member function type that is the top-level type of a declaration or definition, or on such a type appearing as a parameter or return type in a function declarator.

As I read that, you can't put an exception-specification on the type-id in a static_cast, which means that a static_cast can only weaken, not strengthen, the exception specification.

The core WG discussed this at the 10/01 meeting and agreed.

Note (March, 2008):

The Evolution Working Group recommended closing this issue with no further consideration. See paper J16/07-0033 = WG21 N2173.