2

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?

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.