Reputation: 933
I am getting confused around this snippet:
constexpr int f(bool b) {
return b ? throw 0 : 0; }
constexpr int f() { return f(true); }
directly from the c++ draft.
The point I am stucked with is why the standard defines as ill-formed the case of a constexpr
function without arguments (stated in the same link).
May anyone clarify?
Upvotes: 2
Views: 194
Reputation: 20631
The key is "if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression". It's not about the function f()
taking no arguments; it's about the fact that there's no set of arguments you could give it that would make it return a usable value - it always calls f(true)
, which throws an exception.
To re-iterate: a constexpr
function without arguments can certainly be well-formed. But for the given example, it is not.
Also of note is "diagnostic not required". That means that a compiler is free to accept the construct anyway. Indeed, GCC compiles the example in your question without complaining.
Upvotes: 8