Fix std.h/ranges.h ambiguity for optional in c++26

This commit is contained in:
Luiz Siqueira 2026-04-30 15:51:38 -03:00
parent 8f20ee6191
commit 4b53fc2024
2 changed files with 25 additions and 0 deletions

View File

@ -14,6 +14,13 @@
# include <tuple>
# include <type_traits>
# include <utility>
// Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC.
# if FMT_CPLUSPLUS >= 201703L
# if FMT_HAS_INCLUDE(<optional>)
# include <optional>
# endif
# endif
#endif
#include "format.h"
@ -487,6 +494,12 @@ struct range_format_kind
is_range<T, Char>::value, detail::range_format_kind_<T>,
std::integral_constant<range_format, range_format::disabled>> {};
#if defined(__cpp_lib_optional_range_support)
template <typename T, typename Char>
struct range_format_kind<std::optional<T>, Char>
: std::integral_constant<range_format, range_format::disabled> {};
#endif
template <typename R, typename Char>
struct formatter<
R, Char,

View File

@ -20,6 +20,10 @@
# include <ranges>
#endif
#if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<optional>)
# include <optional>
#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<std::optional<int>, char>::value),
fmt::range_format_disabled);
}
#endif
struct vector_string : std::vector<char> {
using base = std::vector<char>;
using base::base;