From 867d768108eb67a6a51bbdb6c1c3f3a74e4e65c5 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 15 May 2023 10:32:31 +1000 Subject: [PATCH] debug: add some constexpr/consteval asserts --- debug/assert.hpp | 9 +++++++++ debug/panic.hpp | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/debug/assert.hpp b/debug/assert.hpp index 04c91cfe..31173b70 100644 --- a/debug/assert.hpp +++ b/debug/assert.hpp @@ -46,6 +46,15 @@ #endif +constexpr void constexpr_assert (auto const &expr) +{ + if (std::is_constant_evaluated ()) + static_cast (expr) ? void (0) : [] () { throw nullptr; } (); + else + assert (expr); +} + + /////////////////////////////////////////////////////////////////////////////// #define SCOPED_SANITY(A) \ ::cruft::debug::scoped_sanity PASTE(__scoped_sanity_checker,__LINE__) ((A)); \ diff --git a/debug/panic.hpp b/debug/panic.hpp index be7147b6..a3c3f96a 100644 --- a/debug/panic.hpp +++ b/debug/panic.hpp @@ -58,6 +58,15 @@ panic [[noreturn]] (const std::string &msg) } +template +constexpr void consteval_panic (void) noexcept { + if constexpr (std::is_same_v) { + throw nullptr; + } +} + + + /////////////////////////////////////////////////////////////////////////////// // not_implemented/unreachable/panic must be callable from constexpr contexts. // but they rely on functions that aren't constexpr to perform the controlled