diff --git a/doc/modules/ROOT/pages/format.adoc b/doc/modules/ROOT/pages/format.adoc index f63ca391..cc9e83e2 100644 --- a/doc/modules/ROOT/pages/format.adoc +++ b/doc/modules/ROOT/pages/format.adoc @@ -101,6 +101,7 @@ See the xref:examples.adoc#examples_fmt_format[formatting example] for a complet To use `std::format`, include `` and ``. The format specifiers described above work with `std::format`. +If you are using `std::format` with pass:[C++26] or newer, `constexpr std::format` is supported. The xref:examples.adoc#examples_fmt_format[formatting example] can be trivially modified by including ``, and replacing all instances of `fmt::` with `std::` diff --git a/include/boost/int128/format.hpp b/include/boost/int128/format.hpp index b66e1735..851e5510 100644 --- a/include/boost/int128/format.hpp +++ b/include/boost/int128/format.hpp @@ -19,6 +19,13 @@ #define BOOST_INT128_HAS_FORMAT +#if defined(__cpp_lib_constexpr_format) && __cpp_lib_constexpr_format >= 202511L +# define BOOST_INT128_HAS_CONSTEXPR_FORMAT +# define BOOST_INT128_CONSTEXPR_FORMAT constexpr +#else +# define BOOST_INT128_CONSTEXPR_FORMAT +#endif + namespace boost::int128::detail { enum class sign_option @@ -236,7 +243,7 @@ struct formatter } template - auto format(T v, FormatContext& ctx) const + BOOST_INT128_CONSTEXPR_FORMAT auto format(T v, FormatContext& ctx) const { char buffer[boost::int128::detail::mini_to_chars_buffer_size]; bool isneg {false}; diff --git a/test/test_format.cpp b/test/test_format.cpp index ee806657..98462ceb 100644 --- a/test/test_format.cpp +++ b/test/test_format.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #ifdef BOOST_INT128_HAS_FORMAT @@ -183,6 +184,14 @@ void test_alignment_negative() BOOST_TEST_CSTR_EQ(std::format("{:*^7d}", T{-42}).c_str(), "**-42**"); } +#ifdef BOOST_INT128_HAS_CONSTEXPR_FORMAT + +using namespace boost::int128::literals; +static_assert(std::format("num: {}", 1234_i128) == "num: 1234"); +static_assert(std::format("num: {}", 1234_u128) == "num: 1234"); + +#endif + int main() { test_empty();