Cassio Neri <
cassi...@gmail.com> writes:
| Hi,
|
| Quoting n3690 (C++14) [expr.const] 5.19/2
|
| A conditional-expression e is a core constant expression unless the
| evaluation of e, following the rules of the abstract machine (1.9), would
| evaluate one of the following expressions:
| [...]
| — a throw-expression (15.1).
|
| Therefore, a constexpr function cannot throw.
That is not correct.
A call to a constexpr function cannot result in an exception if the call
appears in a constexpr context. Otherwise, it is perfectively fine.
| I couldn't find anything that
| says a constexpr function is implicitly declared noexcept. Shouldn't it be
| the case?
This was indeed considered, but abandonned as, in practice, it would
prevent programmers from using 'checking expressions'.
| One selling point of noexcept is that it allows the compiler to generate
| faster code. So we could have that for all constexpr functions.
|
| A possibly reason for not doing making constexpr => noexcept is the recent
| history of constexrp => const in C++11 which is no longer the case in C++14.
| (and this will break code). However, since noexcept is not part of the
| signature (correct me if I'm wrong) the consquences of, if one day,
| constexpr functions are allowed to throw seem less dramatic (it could still
| break code).
|
| Your thoughts?
try:
#include <stdexcept>
constexpr int check(int x) {
return x > 0 ? x : std::domain_error("non-positive value");
}
int main() {
enum E { e = check(1) };
return e;
}
-- Gaby