This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/53473] [C++11] static constexpr noexcept cannot be specialized
- From: "daniel.kruegler at googlemail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 25 May 2012 09:07:20 +0000
- Subject: [Bug c++/53473] [C++11] static constexpr noexcept cannot be specialized
- Auto-submitted: auto-generated
- References: <bug-53473-4@http.gcc.gnu.org/bugzilla/>
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.