0

Looking at the C++11 Spec (n3485) section 5.3.7, note 3 says that the result of noexcept(expr) is false if:

... a potentially-evaluated call to a function... that does not have a non-throwing exception-specification ... a potentially-evaluated throw-expression ... a potentially-evaluated dynamic_cast ... a potentially-evaluated typeid expression...

Does "potentially evaluated" mean that it drills down (not at all? a little?) to determine if one of the conditions can result in false?

I'm finding that (in test code, not an application) a function that claims to be noexcept but does, in fact, throw (even if in all cases) will still be considered to be noexcept. Am misunderstanding the spec or is the code in the following example all wrong?

double calculate(....) noexcept { throw "haha"; }  // using simpsons::nelson

bool does_not_throw = noexcept(calculate());

According to Clang 3.3 this test says that calculate() does not throw.

Arbalest
  • 1,095
  • 11
  • 23
  • 1
    `noexcept` doesn't mean "nothing in this function or stuff it calls can throw" it means "the language guarantees to you that no exception will ever escape this function". If it must, the runtime will enforce that guarantee by killing the program with `std::terminate` instead of allowing an exception to escape a `noexcept` function. (C++11 15.4/9) – Casey Aug 20 '13 at 04:10

1 Answers1

0

All it's doing is checking what the expression does to see if the terms of the expression would throw an exception. It doesn't check the actual code that would potentially be called. If one of the expression terms is a function call that is not explicitly noexcept, then it is assumed to be able to throw exceptions.

Or, to put it another way, it checks to see if all of the functions being called in the expression are noexcept. That's all.

According to Clang 3.3 this test says that calculate() does not throw.

And that's true. Because calculate is defined as noexcept, if it tries to emit an exception, std::terminate will be called. Therefore, no exceptions will be emitted by the function.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982