There is a well-known trick to cause a compile-time error in the evaluation of a constexpr
function by doing something like this:
constexpr int f(int x) {
return (x != 0) ? x : throw std::logic_error("Oh no!");
}
And if the function is used in a constexpr
context you will get a compile-time error if x == 0
. If the argument to f
is not constexpr
, however, then it will throw an exception at run time if x == 0
, which may not always be desired for performance reasons.
Similar to the theory of assert
being guarded by NDEBUG
, is there a way to cause a compile-time error with a constexpr
function, but not do anything at run time?
Finally, do relaxed constexpr
rules in C++1y (C++14) change anything?