Title
Can static_cast drop exception specifications?
Status
nad
Section
7.6.1.9 [expr.static.cast]
Submitter
Steve Adamczyk

Created on 2001-06-27.00:00:00 last changed 196 months ago

Messages

Date: 2002-10-15.00:00:00

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

Date: 2008-03-15.00:00:00

Note (March, 2008):

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

Date: 2022-11-20.07:54:16

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.

History
Date User Action Args
2008-03-17 00:00:00adminsetmessages: + msg1630
2008-03-17 00:00:00adminsetstatus: extension -> nad
2002-11-08 00:00:00adminsetmessages: + msg792
2002-11-08 00:00:00adminsetstatus: nad -> extension
2001-11-09 00:00:00adminsetstatus: open -> nad
2001-06-27 00:00:00admincreate