From 5bec6e6a7d77b136a7542a146b8e5b6da624e579 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Mon, 3 Nov 2025 20:14:55 -0700 Subject: [PATCH] :bug: Fix `STATIC_ASSERT` in non-local context Problem: - The lambda used in `STATIC_ASSERT` has a default capture spec, which is not allowed in non-local contexts. Solution: - Remove the capture spec. It's not needed. --- include/stdx/static_assert.hpp | 4 ++-- test/fail/static_assert.cpp | 2 ++ test/fail/static_assert_format.cpp | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/stdx/static_assert.hpp b/include/stdx/static_assert.hpp index 5fe2c14..7f3be39 100644 --- a/include/stdx/static_assert.hpp +++ b/include/stdx/static_assert.hpp @@ -31,7 +31,7 @@ template constexpr auto ct_check = ct_check_t{}; // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define STATIC_ASSERT(cond, ...) \ - [&]() -> bool { \ + []() -> bool { \ STDX_PRAGMA(diagnostic push) \ STDX_PRAGMA(diagnostic ignored "-Wunknown-warning-option") \ STDX_PRAGMA(diagnostic ignored "-Wc++26-extensions") \ @@ -45,7 +45,7 @@ template constexpr auto ct_check = ct_check_t{}; // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define STATIC_ASSERT(cond, ...) \ - [&]() -> bool { \ + []() -> bool { \ constexpr auto S = STDX_CT_FORMAT(__VA_ARGS__); \ stdx::detail::ct_check.template emit(); \ return B; \ diff --git a/test/fail/static_assert.cpp b/test/fail/static_assert.cpp index 794d18e..f66f6b5 100644 --- a/test/fail/static_assert.cpp +++ b/test/fail/static_assert.cpp @@ -5,6 +5,8 @@ constexpr auto msg = stdx::ct_string{"01234567890123456789012345678901234567890123456789"}; +static_assert(STATIC_ASSERT(true, "not emitted")); + auto main() -> int { static_assert(STATIC_ASSERT(true, "not emitted")); STATIC_ASSERT(false, msg); diff --git a/test/fail/static_assert_format.cpp b/test/fail/static_assert_format.cpp index 431349d..1fdc859 100644 --- a/test/fail/static_assert_format.cpp +++ b/test/fail/static_assert_format.cpp @@ -3,6 +3,8 @@ // EXPECT: hello world int 123 +static_assert(STATIC_ASSERT(true, "hello {} {} {}", "world", int, 42)); + template constexpr auto f() { STATIC_ASSERT(false, "hello {} {} {}", "world", T, 123); }