From 58f16b817f10a5b7e0aac1a6c0607ec152fcd6f6 Mon Sep 17 00:00:00 2001 From: lukas <89145039+codethinki@users.noreply.github.com> Date: Sat, 17 Oct 2026 00:06:26 +0200 Subject: [PATCH 1/2] refactor: not_null constexpr in debug builds The constructor and checkVal were constexpr in release builds only (CTH_RELEASE_CONSTEXPR). In constant evaluation the CTH_CRITICAL check only runs its failure branch for a null value, so a valid value evaluates at compile time and a null value is a compile error. CTH_RELEASE_CONSTEXPR had no other use and is removed, the cthm copy too. --- cth/incl/cth/constants.hpp | 2 -- cth/incl/cth/ptr/not_null.hpp | 4 ++-- cth/tests/src/pointers/not_null_test.cpp | 8 ++++++++ cth_cthm/incl/cthm/macro/compilation.hpp | 2 -- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cth/incl/cth/constants.hpp b/cth/incl/cth/constants.hpp index 451a7d3..843df57 100644 --- a/cth/incl/cth/constants.hpp +++ b/cth/incl/cth/constants.hpp @@ -34,14 +34,12 @@ #define CTH_DEBUG_INLINE_IMPL(code) { code } #define CTH_RELEASE_NOEXCEPT noexcept(false) -#define CTH_RELEASE_CONSTEXPR #else #define CTH_DEBUG_IMPL = default; #define CTH_DEBUG_INLINE_IMPL(code) \ {} #define CTH_RELEASE_NOEXCEPT noexcept(true) -#define CTH_RELEASE_CONSTEXPR constexpr #endif #include diff --git a/cth/incl/cth/ptr/not_null.hpp b/cth/incl/cth/ptr/not_null.hpp index 3a1c5e8..50c3eb7 100644 --- a/cth/incl/cth/ptr/not_null.hpp +++ b/cth/incl/cth/ptr/not_null.hpp @@ -40,12 +40,12 @@ class not_null { static constexpr bool NOEXCEPT_CHECK_VAL = COMPILATION_MODE != CompilationMode::DEBUG; static constexpr bool RELEASE_VAL_NOEXCEPT = NOEXCEPT_ASSIGN && NOEXCEPT_MOVE_CTOR; - static CTH_RELEASE_CONSTEXPR void checkVal(T const& obj) noexcept(NOEXCEPT_CHECK_VAL) { + static constexpr void checkVal(T const& obj) noexcept(NOEXCEPT_CHECK_VAL) { CTH_CRITICAL(obj == Null, "obj must not be Null") {} } public: - CTH_RELEASE_CONSTEXPR not_null(T obj) noexcept(NOEXCEPT_MOVE_CTOR && NOEXCEPT_CHECK_VAL) : + constexpr not_null(T obj) noexcept(NOEXCEPT_MOVE_CTOR && NOEXCEPT_CHECK_VAL) : _obj{std::move(obj)} { not_null::checkVal(_obj); } template requires(std::convertible_to && !std::same_as) diff --git a/cth/tests/src/pointers/not_null_test.cpp b/cth/tests/src/pointers/not_null_test.cpp index 2538624..2fd432d 100644 --- a/cth/tests/src/pointers/not_null_test.cpp +++ b/cth/tests/src/pointers/not_null_test.cpp @@ -14,6 +14,14 @@ struct TestStruct { auto const data = std::make_unique(EXAMPLE_NUMBER); // NOLINT(cert-err58-cpp) +CTH_TEST(not_null, constexpr) { + static_assert([] { + not_null const ptr{&EXAMPLE_NUMBER}; + not_null const copy{ptr}; + return *copy; + }() == EXAMPLE_NUMBER); +} + CTH_TEST(not_null, comparison) { int a = EXAMPLE_NUMBER; int b = EXAMPLE_NUMBER; diff --git a/cth_cthm/incl/cthm/macro/compilation.hpp b/cth_cthm/incl/cthm/macro/compilation.hpp index bd98362..0e0bde7 100644 --- a/cth_cthm/incl/cthm/macro/compilation.hpp +++ b/cth_cthm/incl/cthm/macro/compilation.hpp @@ -36,13 +36,11 @@ #define CTH_DEBUG_INLINE_IMPL(code) {code} #define CTH_RELEASE_NOEXCEPT noexcept(false) -#define CTH_RELEASE_CONSTEXPR #else #define CTH_DEBUG_IMPL = default; #define CTH_DEBUG_INLINE_IMPL(code) \ {} #define CTH_RELEASE_NOEXCEPT noexcept(true) -#define CTH_RELEASE_CONSTEXPR constexpr #endif #include From e5ae82c851251dcae99285b61275976a1282f8e9 Mon Sep 17 00:00:00 2001 From: lukas <89145039+codethinki@users.noreply.github.com> Date: Sat, 17 Oct 2026 00:06:48 +0200 Subject: [PATCH 2/2] leave cthm alone, a separate PR removes it --- cth_cthm/incl/cthm/macro/compilation.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cth_cthm/incl/cthm/macro/compilation.hpp b/cth_cthm/incl/cthm/macro/compilation.hpp index 0e0bde7..bd98362 100644 --- a/cth_cthm/incl/cthm/macro/compilation.hpp +++ b/cth_cthm/incl/cthm/macro/compilation.hpp @@ -36,11 +36,13 @@ #define CTH_DEBUG_INLINE_IMPL(code) {code} #define CTH_RELEASE_NOEXCEPT noexcept(false) +#define CTH_RELEASE_CONSTEXPR #else #define CTH_DEBUG_IMPL = default; #define CTH_DEBUG_INLINE_IMPL(code) \ {} #define CTH_RELEASE_NOEXCEPT noexcept(true) +#define CTH_RELEASE_CONSTEXPR constexpr #endif #include