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
1 change: 1 addition & 0 deletions doc/modules/ROOT/pages/format.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ See the xref:examples.adoc#examples_fmt_format[formatting example] for a complet

To use `std::format`, include `<boost/int128/format.hpp>` and `<format>`.
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 `<format>`,
and replacing all instances of `fmt::` with `std::`
9 changes: 8 additions & 1 deletion include/boost/int128/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -236,7 +243,7 @@ struct formatter<T>
}

template <typename FormatContext>
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};
Expand Down
9 changes: 9 additions & 0 deletions test/test_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <boost/int128.hpp>
#include <boost/int128/format.hpp>
#include <boost/int128/literals.hpp>
#include <boost/core/lightweight_test.hpp>

#ifdef BOOST_INT128_HAS_FORMAT
Expand Down Expand Up @@ -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<boost::int128::uint128_t>();
Expand Down
Loading