diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index b1455977c65..27253f50ea8 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -5634,9 +5634,14 @@ namespace __format typename basic_format_context<_Out, _CharT>::iterator format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc) const { - using __maybe_const_range - = __format::__maybe_const_range<_Rg, _CharT>; - return _M_format<__maybe_const_range>(__rg, __fc); + using _Range = remove_reference_t<_Rg>; + if constexpr (__format::__const_formattable_range<_Range, _CharT>) + { + using _CRef = ranges::range_reference_t; + if constexpr (same_as, _Tp>) + return _M_format(__rg, __fc); + } + return _M_format(__rg, __fc); } private: diff --git a/libstdc++-v3/testsuite/std/format/ranges/formatter.cc b/libstdc++-v3/testsuite/std/format/ranges/formatter.cc index a4f5d9210dd..00ce9f6dd0c 100644 --- a/libstdc++-v3/testsuite/std/format/ranges/formatter.cc +++ b/libstdc++-v3/testsuite/std/format/ranges/formatter.cc @@ -1,5 +1,6 @@ // { dg-do run { target c++23 } } +#include #include #include #include @@ -138,6 +139,26 @@ test_nested() VERIFY( res == "+<01; 02; 11; 12>+" ); } +struct MyFlatMap : std::flat_map +{ + using std::flat_map::flat_map; +}; + +template +struct std::formatter + // This cannot apply format BitVector const&, because formatted type would + // be std::pair, and formatter for + // pair cannot format it. + : std::range_formatter +{}; + +void test_const_ref_type_mismatch() +{ + MyFlatMap m{{1, 11}, {2, 22}}; + std::string res = std::format("{:m}", m); + VERIFY( res == "{1: 11, 2: 22}" ); +} + template using VectorFormatter = std::formatter, CharT>; @@ -146,4 +167,5 @@ int main() test_outputs(); test_outputs(); test_nested(); + test_const_ref_type_mismatch(); }