http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53473

--- Comment #3 from Daniel Krügler <daniel.kruegler at googlemail dot com> 
2012-05-25 09:07:20 UTC ---
(In reply to comment #2)
> Does the standard allow exceptions in constexpr? A throw is not exactly a
> return statement, but according to the rule "constexpr function shall satisfy
> [...] exactly one return statement" I'd expect a constexpr function can never
> throw anyway. Thus the noexcept itself seems to make no sense in the first
> place.

No, both concepts of constant expressions and exception-specifications are
independent decisions. The following is a perfectly valid constexpr function:

#include <stdexcept>

constexpr int validating_abs(int val) noexcept(false)
{ return val < 0 ? throw std::runtime_error("negative") : val; }

int main()
{
  constexpr int v1 = validating_abs(1); // OK
  constexpr int v2 = validating_abs(-1); // Error
  int v = -1;
  try {
    int v3 = validating_abs(v); // OK, runtime validation
  } catch (std::runtime_error&) {}
}

It is just a fact, that the "effective expression" is relevant when we consider
constant expressions and throw expressions, like for v2. But constexpr
functions can also be called in non-constant contexts - like for v3 - where
this restriction does not exist.

Reply via email to