I previously asked about function overloading based on whether the arguments are constexpr. I\'m trying to work around the disappointing answer to that question to make a sm
How is the answer to the other question disappointing? It implements almost exactly what you presently describe, except for the way the compiler prints the text in the diagnostic message.
The reason it needs to be done with throw
is that compile-time evaluation of constexpr
in a context that could be evaluated at runtime is optional. For example, the implementation could choose to let you step through the constexpr
code in debugging mode.
constexpr
is a weak attribute of functions (declaration specifier) that cannot change the resulting value of an expression using the function. It is a guarantee that the semantic meaning at runtime is fixed at compile time, but doesn't allow you to specify a special compile-time shortcut.
As for flagging invalid conditions, throw
is a subexpression which is invalid as a constant expression, except when hidden in the unevaluated side of ?:
, &&
, or ||
. The language guarantees that this will be flagged at compile time, even if the debugger lets you step through it at runtime, and only if the flag is really triggered.
The language gets things right here. Unfortunately that cannot be reconciled with the special diagnostic message feature of static_assert
or branching on constexpr
-ness.