The problem
Using static_assert to generate compile-time error is not always easy because it requires a constant expression as first argument. I found, on StackOverflow, several example where throw was used instead of static_assert:
static_assert(<constante evaluated condition>,"message");
if (<condition>) {throw "message";}
My question is, is it legal to do that inside constexpr or consteval function.
For instance, if I want to check at compile time if some integer is positive (pretty dumb...) I may write:
CONSTSPEC void checkpos(int x) {
if (x < 0) {
// is this ill-formed, no diagnostic required (when called with a
// negative argument?)
throw "argument must be positive";
}
}
Where CONSTSPEC can be constexpr or consteval.
With constexpr
By looking at https://timsong-cpp.github.io/cppwp/n4861/dcl.constexpr#6 and also this answer it seems that calling checkpos with a strictly negative argument is ill-formed but no diagnostic is required, letting a compiler do whatever he wants, which would be useless for compile-time error detection.
With consteval
I can't find a word in the standard. Should I understand that then any call is well-formed and passing a strictly negative argument will result in a mandatory compile-time error?
Question
Is throw usable inside a constexpr function (I don't think so) or inside a consteval function (maybe) to generate a compile-time error?
throwbeing invalid forever.voidas returned type, the section I am referencing simply does not apply. So no IFNDR, I just don't get now what is the mandate behavior of a "compile-time" throw (pre-C++26 wrt to cigien comment)throwmakes(made) the expression not a constant expression; with the paper, it is the fact to not catch that exception which makes not a constant expression.