From 9e7b7dd6d953e7f74b6ea2862e37beec0acaa9cb Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Tue, 20 May 2025 10:17:58 -0600 Subject: [PATCH] :white_check_mark: :sound: Improve `ct_format` help message Problem: - When a call to `ct_format` has a mismatch between the number of format specifiers and the number of arguments, the help message is obscure. Solution: - Provide a clearer help message. --- include/stdx/ct_format.hpp | 4 ++++ test/fail/CMakeLists.txt | 1 + test/fail/ct_format_mismatch.cpp | 5 +++++ 3 files changed, 10 insertions(+) create mode 100644 test/fail/ct_format_mismatch.cpp diff --git a/include/stdx/ct_format.hpp b/include/stdx/ct_format.hpp index d9beace..09c6771 100644 --- a/include/stdx/ct_format.hpp +++ b/include/stdx/ct_format.hpp @@ -221,6 +221,10 @@ constexpr auto ct_format = [](auto &&...args) { using data = detail::fmt_data; + static_assert(data::N == sizeof...(args), + "Format string has a mismatch between the number of format " + "specifiers and arguments."); + [[maybe_unused]] auto const format1 = [&](auto &&arg) { constexpr auto cts = detail::to_ct_string(data::splits[I]); diff --git a/test/fail/CMakeLists.txt b/test/fail/CMakeLists.txt index 8d0920a..10026c8 100644 --- a/test/fail/CMakeLists.txt +++ b/test/fail/CMakeLists.txt @@ -59,6 +59,7 @@ if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20) add_fail_tests( atomic_bool_dec + ct_format_mismatch dynamic_span_no_ct_capacity dynamic_container_no_ct_capacity tuple_index_out_of_bounds diff --git a/test/fail/ct_format_mismatch.cpp b/test/fail/ct_format_mismatch.cpp new file mode 100644 index 0000000..51f580a --- /dev/null +++ b/test/fail/ct_format_mismatch.cpp @@ -0,0 +1,5 @@ +#include + +// EXPECT: mismatch between the number of format specifiers and arguments + +auto main() -> int { [[maybe_unused]] auto x = stdx::ct_format<"Hello">(42); }