Boost logo

Boost :

Subject: Re: [boost] The noexcept Specifier & Block
From: Anders Dalvander (boost_at_[hidden])
Date: 2009-05-04 12:40:25


Posted the following to std.lang.c++, cross-posting here, mostly
directed to David Abrahams:

After reading N2855 - Rvalue References and Exception Safety
(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2855.html) I'm
still not confident that a new keyword `noexcept` would be optimal.
Especially when the proposal combines this new keyword with a somewhat
enhanced version of the already existing exception specification
`throw(...)` to explicitly indicate that a move constructor or a
destructor may throw exceptions. And at the same time propose to
deprecate exception specifications altogether.

I think that it would be better to use a simplified exception specification:

- `throw()` to indicate that a function does not throw any exception,
which is statically checked by the compiler. This would behave as
`noexcept` in the N2855 proposal.
- `throw(...)` to indicate that a function may throw any exception. Main
use for throwing move constructors and throwing destructors.
- Any other exception specification would be ill-formed and thus would
require a compiler diagnostic.

Update the new exception specification to behave as `noexcept` in N2855,
where needed.

Unfortunately I don't have a really good idea to implement something
similar to the `noexcept` block as described in N2855. Perhaps a `try`
block without a `catch` handler, perhaps someone else has a better idea?

double sqrt(double); // may throw exceptions

void f(double &x) throw() {
  if (x > 0) {
    x = sqrt(x); // ill-formed: sqrt(x) might throw an exception!
  }
}

void g(double &x) throw() {
  if (x > 0) {
    try { x = sqrt(x); } // okay: if sqrt(x) throws, invokes undefined
behavior
  }
}

Rationale:
- Exception specification as of C++98/C++03 are considered broken and
discouraged. No need to deprecate, remove it entirely.
- `throw()` is already used by a lot of libraries, for example boost, to
indicate nothrow functions. With this proposal these functions would
behave as `noexcept` with no change.
- No new keywords are needed.

Remove the following:

exception-specification:
  throw ( [type-id-list]opt )

type-id-list:
  type-id
  type-id-list , type-id

Add the following:

no-throw-exception-specification:
  throw ( )
    
any-throw-exception-specification:
  throw ( ... )

What do you think?

Regards,
Anders Dalvander


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk