14

While going through the last edits of the C++0x Working draft I found a lot of

  • removal of the keyword noexcept
  • addition of textual Throws: nothing at the same place

and vice versa. Just some examples:

  • replacement of noexcept against Throws: nothing: 20.6.4 Pointer safety [util.dynamic.safety] template<class T> T*undeclare_reachable(T*p);
  • addition of noexcept: 20.6.3.2. Pointer traits member functions [pointer.traits.functions]: static pointer pointer_trait<T*>::pointer_to(...) noexcept;

Questions here:

  • Is there a general rule/pattern, when we will find noexcept vs. Throws: nothing in the Std-Lib?
  • Should users derive a specific behavior by that given rule? I.e. when they should or should not add noexcept to their own functions?
iammilind
  • 68,093
  • 33
  • 169
  • 336
towi
  • 21,587
  • 28
  • 106
  • 187

1 Answers1

16

In Madrid we were strongly influenced by N3279 which includes the following guidelines:

Adopted Guidelines

  • No library destructor should throw. They shall use the implicitly supplied (non- throwing) exception specification.

  • Each library function having a wide contract, that the LWG agree cannot throw, should be marked as unconditionally noexcept.

  • If a library swap function, move-constructor, or move-assignment operator is conditionally-wide (i.e. can be proven to not throw by applying the noexcept operator) then it should be marked as conditionally noexcept. No other function should use a conditional noexcept specification.

  • Library functions designed for compatibility with “C” code (such as the atomics facility), may be marked as unconditionally noexcept.

I would not interpret these guidelines as necessarily targeting a wider audience. This is mainly an admission that we do have backward compatibility concerns with adding noexcept. If we get it wrong, noexcept is easier to add than to remove in the next standard. So we attempted an application of noexcept that was both conservative and systematic.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • I see. Difficult situation. I think I will read a bit more in the WG21 papers to prepare myself for coming questions like *"when should I add `noexcept` to my own functions?".* N3279 is a good entry point. Do you have any more? For example, currently I suggested `swap` should be `noexcept` in user-code, and planned to follow the literature about *move-assign*, etc. Should users mark theirs `noexcept` or is the implicit assumption enough? Can we expect compilers to gain (someday) anything from it, of users do? – towi Apr 22 '11 at 14:33
  • I do not currently have access to a compiler which implements noexcept. Therefore I do not have any personal advice on the matter (experimentation is a big thing with me). You can find all of the WG21 papers here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/ – Howard Hinnant Apr 22 '11 at 14:52
  • Thanks, I will follow the compiler notes closely. Yes, I am deep into those papers already :-) – towi Apr 22 '11 at 15:15
  • The June 2011 Overload Journal (http://accu.org/var/uploads/journals/overload103.pdf) has an excellent article explaining the semantics of noexcept in detail. – Sumant Aug 17 '11 at 22:30