Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/stdx/static_assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ template <ct_string Fmt, auto... Args> constexpr auto static_format() {
} // namespace v1
} // namespace stdx

#if __cpp_static_assert >= 202306L
#define STATIC_ASSERT(cond, ...) \
[]<bool B>() -> bool { \
static_assert( \
B, std::string_view{stdx::detail::static_format<__VA_ARGS__>()}); \
return B; \
}.template operator()<cond>()
#else
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define STATIC_ASSERT(cond, ...) \
[]<bool B>() -> bool { \
Expand All @@ -46,3 +54,4 @@ template <ct_string Fmt, auto... Args> constexpr auto static_format() {
}.template operator()<cond>()

#endif
#endif
14 changes: 14 additions & 0 deletions test/fail/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ function(add_formatted_error_tests)
add_fail_tests(static_assert_format)
endfunction()

function(add_26_formatted_error_tests)
add_compile_fail_test("static_assert.cpp" NAME static_assert_26 LIBRARIES
stdx)
add_compile_fail_test("static_assert_format.cpp" NAME
static_assert_format_26 LIBRARIES stdx)
target_compile_features(EXPECT_FAIL.static_assert_26 PRIVATE cxx_std_26)
target_compile_features(EXPECT_FAIL.static_assert_format_26
PRIVATE cxx_std_26)
endfunction()

if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"
AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL 15)
Expand All @@ -42,6 +52,10 @@ if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20)
add_formatted_error_tests()
endif()

if("cxx_std_26" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
add_26_formatted_error_tests()
endif()

add_fail_tests(
atomic_bool_dec
dynamic_span_no_ct_capacity
Expand Down
Loading