libassert
libassert copied to clipboard
Unconditional assertion that does not cause [[noreturn]]
Hi and thanks for the amazing library!
For handling unexpected values, I would like an unconditional assertion for which a custom assertion handler can decide not to abort, i.e. one that is not annotated with [[noreturn]]. Something like:
enum_value ToEnum(std::uint8_t byte)
{
switch (value) {
case 0x00: return enum_value::first;
case 0x01: return enum_value::second;
}
UNEXPECTED(byte); // Like PANIC(byte) but can continue if the assertion handler does not abort
return enum_value::unexpected;
}
I want to be able to crash in debug (to make it obvious during testing) but in release, report the value via the assertion handler and continue (to avoid crashing the app for users).
If this is something you'll be interested in as well, I can open a PR for that.
Hi, thanks for opening this. I can see the use-case, though the best way to go about this is not entirely clear to me. I'm thinking of questions like does this require maybe-returning variants of both panic and assertions? How could this be clearly introduced without confusion? What would the failure handler interface look like? Maybe some sort of DEBUG_PANIC that's checked in debug and not in release is the solution here?
Well, the assertion handler can already return from fail and the documentation states that it can do that unless it's PANIC or UNREACHABLE so this will be a variant of process_panic but without [[noreturn]] and LIBASSERT_PRIMITIVE_PANIC at the end, like process_assert_fail.