debug: add some constexpr/consteval asserts

This commit is contained in:
Danny Robson 2023-05-15 10:32:31 +10:00
parent 4c1fb3889a
commit 867d768108
2 changed files with 18 additions and 0 deletions

View File

@ -46,6 +46,15 @@
#endif #endif
constexpr void constexpr_assert (auto const &expr)
{
if (std::is_constant_evaluated ())
static_cast<bool> (expr) ? void (0) : [] () { throw nullptr; } ();
else
assert (expr);
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define SCOPED_SANITY(A) \ #define SCOPED_SANITY(A) \
::cruft::debug::scoped_sanity PASTE(__scoped_sanity_checker,__LINE__) ((A)); \ ::cruft::debug::scoped_sanity PASTE(__scoped_sanity_checker,__LINE__) ((A)); \

View File

@ -58,6 +58,15 @@ panic [[noreturn]] (const std::string &msg)
} }
template <typename TagT = void>
constexpr void consteval_panic (void) noexcept {
if constexpr (std::is_same_v<TagT, void>) {
throw nullptr;
}
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// not_implemented/unreachable/panic must be callable from constexpr contexts. // not_implemented/unreachable/panic must be callable from constexpr contexts.
// but they rely on functions that aren't constexpr to perform the controlled // but they rely on functions that aren't constexpr to perform the controlled