How to fail a consteval function?
4
I have the following function:
template <size_t TSize>
consteval size_t indexOf(SomeEnum someEnum,
const std::array<SomeEnum, TSize> &arr) {
for (size_t i = 0; i < TSize; ++i) {
if (arr[i] == someEnum) {
return i;
}
}
// How to fail here?
return SOME_DEFAULT_WRONG_VALUE;
}
The function should fail instead of returning a default value, but I can't throw an exception or call assert
. I can add a static_assert
to every call to the function (with a macro it will be less horrible), but I'd prefer a solution that works in the function. Is there a way to trigger a compilation failure in such a scenario?
c++ c++20 consteval
assert
? – chris 15 hours agothrow
from aconstexpr
. I'm not familiar enough withconsteval
to be certain, but I believe you should be able tothrow
from them as well. – François Andrieux 15 hours agoconsteval
or if you have an external requirement that you not usethrow
? – François Andrieux 15 hours ago