From 4b53fc202447a945818008ab5b9b7c473ddb3748 Mon Sep 17 00:00:00 2001 From: Luiz Siqueira Date: Thu, 30 Apr 2026 15:51:38 -0300 Subject: [PATCH] Fix std.h/ranges.h ambiguity for optional in c++26 --- include/fmt/ranges.h | 13 +++++++++++++ test/ranges-test.cc | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index d7dbc168..26f7895d 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -14,6 +14,13 @@ # include # include # include + +// Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC. +# if FMT_CPLUSPLUS >= 201703L +# if FMT_HAS_INCLUDE() +# include +# endif +# endif #endif #include "format.h" @@ -487,6 +494,12 @@ struct range_format_kind is_range::value, detail::range_format_kind_, std::integral_constant> {}; +#if defined(__cpp_lib_optional_range_support) +template +struct range_format_kind, Char> + : std::integral_constant {}; +#endif + template struct formatter< R, Char, diff --git a/test/ranges-test.cc b/test/ranges-test.cc index bd161d75..2f35f293 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -20,6 +20,10 @@ # include #endif +#if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE() +# include +#endif + #include "fmt/format.h" #include "gtest/gtest.h" @@ -271,6 +275,14 @@ TEST(ranges_test, disabled_range_formatting_of_path) { fmt::range_format::disabled); } +# if defined(__cpp_lib_optional_range_support) +TEST(ranges_test, disabled_range_formatting_of_optional) { + // Range format of optional is disabled to avoid ambiguity with fmt/std.h. + EXPECT_EQ((fmt::range_format_kind, char>::value), + fmt::range_format_disabled); +} +#endif + struct vector_string : std::vector { using base = std::vector; using base::base;