Is it possible to implement a thing such as "constexpr assert"
on bare metal?
I would normally use a throw
statement as it is mentioned here for example. But the compiler rejects the code because of --fno-exceptions
even though throw
is used only in a constant evaluated context.
My implementation:
inline constexpr void constexpr_assert(bool condition)
{
if (std::is_constant_evaluated())
{
if (!condition)
{
throw; // error: exception handling disabled, use '-fexceptions' to enable
}
}
else
{
assert(condition);
}
}
Potential usage:
class EventMask
{
uint32_t mask;
public:
static constexpr auto MAX_ID = 31;
constexpr EventMask(unsigned int event_id)
: mask(static_cast<uint32_t>(1u) << event_id)
{
constexpr_assert(event_id <= MAX_ID);
}
};
int main(int argc, char** argv)
{
EventMask s_ev(55); // this should fail on compilation time
EventMask d_ev(argc); // this could fail in the runtime
}
My compiler is (GNU Arm Embedded Toolchain 10.3-2021.10) 10.3.1