Related:
- How to get a compile time error in constant evaluated expression?
- Enable static checks for constant evaluation
- How to fail a consteval function?
I want to have an assert macro that when possible is a static_assert
, but falls back to assert
when static_assert
isn't possible. My sense is that std::is_constant_evaluated()
should help with this, by being intended to allow constexper execution to use a different implementation than runtime execution, but everything I've tried like that fails:
#define ASSERT0(cond) \
if (std::is_constant_evaluated()) { \
static_assert((cond)); \
} else { \
assert((cond)); \
}
#define ASSERT1(cond) \
if constexpr (std::is_constant_evaluated()) { \
static_assert((cond)); \
} else { \
assert((cond)); \
}
#define ASSERT2(cond) \
if consteval { \
static_assert((cond)); \
} else { \
assert((cond)); \
}
https://godbolt.org/z/KzWfxMdMd
Fundamentally, as @BoP points out, the challenge is: when a function is being evaluated in a constexpr context, its arguments still aren't integral constants (https://godbolt.org/z/co93nc59h).
I'm new to std::is_constant_evaluated()
. Is there something I'm missing? Should it be able to be used to switch between static_assert
and assert
?