Skip to content

Commit 3c75f79

Browse files
Implement formatter specializations for container adaptors (#4825)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 041f584 commit 3c75f79

File tree

9 files changed

+1288
-9
lines changed

9 files changed

+1288
-9
lines changed

stl/inc/format

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4774,6 +4774,38 @@ public:
47744774
private:
47754775
_Fill_align_and_width_specs<_CharT> _Specs;
47764776
};
4777+
4778+
// Per LWG-3997, `_CharT` in library-provided `formatter` specializations is
4779+
// constrained to character types supported by `format`.
4780+
template <class _AdaptorType, _Format_supported_charT _CharT, template <class> class _RefView>
4781+
struct _Adaptor_formatter_base {
4782+
private:
4783+
using _Container = _AdaptorType::container_type;
4784+
using _Maybe_const_container = _Fmt_maybe_const<_Container, _CharT>;
4785+
using _Maybe_const_adaptor = _Maybe_const<is_const_v<_Maybe_const_container>, _AdaptorType>;
4786+
4787+
formatter<_RefView<_Maybe_const_container>, _CharT> _Underlying;
4788+
4789+
public:
4790+
template <class _ParseCtx>
4791+
constexpr _ParseCtx::iterator parse(_ParseCtx& _Ctx) {
4792+
return _Underlying.parse(_Ctx);
4793+
}
4794+
4795+
template <class _FmtCtx>
4796+
_FmtCtx::iterator format(_Maybe_const_adaptor& _Adaptor, _FmtCtx& _Ctx) const {
4797+
struct _Container_exposer : _AdaptorType {
4798+
using _AdaptorType::c;
4799+
4800+
_Container_exposer(const _Container_exposer&) = delete;
4801+
_Container_exposer& operator=(const _Container_exposer&) = delete;
4802+
~_Container_exposer() = delete;
4803+
};
4804+
4805+
constexpr auto _Mem_cont_ptr = &_Container_exposer::c;
4806+
return _Underlying.format(_Adaptor.*_Mem_cont_ptr, _Ctx);
4807+
}
4808+
};
47774809
#endif // _HAS_CXX23
47784810
_STD_END
47794811

stl/inc/queue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#if _HAS_CXX23
2020
#include <__msvc_ranges_to.hpp>
21+
#include <format>
2122
#include <iterator>
2223
#endif // _HAS_CXX23
2324

@@ -223,6 +224,14 @@ void swap(queue<_Ty, _Container>& _Left, queue<_Ty, _Container>& _Right) noexcep
223224
template <class _Ty, class _Container, class _Alloc>
224225
struct uses_allocator<queue<_Ty, _Container>, _Alloc> : uses_allocator<_Container, _Alloc>::type {};
225226

227+
#if _HAS_CXX23
228+
// Per LWG-3997, `_CharT` in library-provided `formatter` specializations is
229+
// constrained to character types supported by `format`.
230+
template <_Format_supported_charT _CharT, class _Ty, formattable<_CharT> _Container>
231+
struct formatter<queue<_Ty, _Container>, _CharT>
232+
: _Adaptor_formatter_base<queue<_Ty, _Container>, _CharT, _RANGES ref_view> {};
233+
#endif // _HAS_CXX23
234+
226235
_EXPORT_STD template <class _Ty, class _Container = vector<_Ty>, class _Pr = less<typename _Container::value_type>>
227236
class priority_queue {
228237
public:
@@ -490,6 +499,13 @@ void swap(priority_queue<_Ty, _Container, _Pr>& _Left, priority_queue<_Ty, _Cont
490499
template <class _Ty, class _Container, class _Pr, class _Alloc>
491500
struct uses_allocator<priority_queue<_Ty, _Container, _Pr>, _Alloc> : uses_allocator<_Container, _Alloc>::type {};
492501

502+
#if _HAS_CXX23
503+
// Per LWG-3997, `_CharT` in library-provided `formatter` specializations is
504+
// constrained to character types supported by `format`.
505+
template <_Format_supported_charT _CharT, class _Ty, formattable<_CharT> _Container, class _Comp>
506+
struct formatter<priority_queue<_Ty, _Container, _Comp>, _CharT>
507+
: _Adaptor_formatter_base<priority_queue<_Ty, _Container, _Comp>, _CharT, _RANGES ref_view> {};
508+
#endif // _HAS_CXX23
493509
_STD_END
494510

495511
#pragma pop_macro("new")

stl/inc/stack

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#if _HAS_CXX23
1313
#include <__msvc_ranges_to.hpp>
14+
#include <format>
1415
#include <iterator>
1516
#endif // _HAS_CXX23
1617

@@ -209,6 +210,14 @@ void swap(stack<_Ty, _Container>& _Left, stack<_Ty, _Container>& _Right) noexcep
209210

210211
template <class _Ty, class _Container, class _Alloc>
211212
struct uses_allocator<stack<_Ty, _Container>, _Alloc> : uses_allocator<_Container, _Alloc>::type {};
213+
214+
#if _HAS_CXX23
215+
// Per LWG-3997, `_CharT` in library-provided `formatter` specializations is
216+
// constrained to character types supported by `format`.
217+
template <_Format_supported_charT _CharT, class _Ty, formattable<_CharT> _Container>
218+
struct formatter<stack<_Ty, _Container>, _CharT>
219+
: _Adaptor_formatter_base<stack<_Ty, _Container>, _CharT, _RANGES ref_view> {};
220+
#endif // _HAS_CXX23
212221
_STD_END
213222

214223
#pragma pop_macro("new")

stl/inc/yvals_core.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@
360360
// P2273R3 constexpr unique_ptr
361361
// P2278R4 cbegin Should Always Return A Constant Iterator
362362
// P2286R8 Formatting Ranges
363-
// (only the '?' format specifier for strings and characters)
364363
// P2291R3 constexpr Integral <charconv>
365364
// P2302R4 ranges::contains, ranges::contains_subrange
366365
// P2321R2 zip
@@ -384,6 +383,7 @@
384383
// P2539R4 Synchronizing print() With The Underlying Stream
385384
// P2540R1 Empty Product For Certain Views
386385
// P2549R1 unexpected<E>::error()
386+
// P2585R1 Improve Default Container Formatting
387387
// P2599R2 mdspan: index_type, size_type
388388
// P2604R0 mdspan: data_handle_type, data_handle(), exhaustive
389389
// P2613R1 mdspan: empty()
@@ -1756,6 +1756,7 @@ _EMIT_STL_ERROR(STL1004, "C++98 unexpected() is incompatible with C++23 unexpect
17561756
#define __cpp_lib_constexpr_typeinfo 202106L
17571757
#define __cpp_lib_containers_ranges 202202L
17581758
#define __cpp_lib_expected 202211L
1759+
#define __cpp_lib_format_ranges 202207L
17591760
#define __cpp_lib_formatters 202302L
17601761
#define __cpp_lib_forward_like 202207L
17611762
#define __cpp_lib_freestanding_expected 202311L

tests/libcxx/expected_results.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,6 @@ std/language.support/support.limits/support.limits.general/cstdlib.version.compi
244244
# P2255R2 "Type Traits To Detect References Binding To Temporaries"
245245
std/language.support/support.limits/support.limits.general/type_traits.version.compile.pass.cpp FAIL
246246

247-
# P2286R8 Formatting Ranges
248-
std/containers/container.adaptors/container.adaptors.format/format.functions.format.pass.cpp FAIL
249-
std/containers/container.adaptors/container.adaptors.format/format.functions.vformat.pass.cpp FAIL
250-
std/containers/container.adaptors/container.adaptors.format/format.pass.cpp FAIL
251-
std/containers/container.adaptors/container.adaptors.format/parse.pass.cpp FAIL
252-
std/containers/container.adaptors/container.adaptors.format/types.compile.pass.cpp FAIL
253-
std/utilities/format/format.formattable/concept.formattable.compile.pass.cpp FAIL
254-
255247

256248
# *** MISSING COMPILER FEATURES ***
257249
# P1169R4 static operator()
@@ -969,6 +961,7 @@ std/input.output/string.streams/stringstream/stringstream.members/str.allocator_
969961
# These formatter tests need to create basic_format_contexts, so test_format_context.h
970962
# says "Please create a vendor specific version of the test functions".
971963
# If we do, some of these tests should be able to work.
964+
std/containers/container.adaptors/container.adaptors.format/format.pass.cpp FAIL
972965
std/containers/sequences/vector.bool/vector.bool.fmt/format.pass.cpp FAIL
973966
std/thread/thread.threads/thread.thread.class/thread.thread.id/format.pass.cpp FAIL
974967
std/utilities/format/format.formatter/format.context/format.context/advance_to.pass.cpp FAIL

tests/std/test.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ tests\P2278R4_const_span
617617
tests\P2278R4_ranges_const_iterator_machinery
618618
tests\P2278R4_ranges_const_range_machinery
619619
tests\P2278R4_views_as_const
620+
tests\P2286R8_text_formatting_container_adaptors
620621
tests\P2286R8_text_formatting_debug_enabled_specializations
621622
tests\P2286R8_text_formatting_escaping
622623
tests\P2286R8_text_formatting_escaping_legacy_text_encoding
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
RUNALL_INCLUDE ..\usual_latest_matrix.lst

0 commit comments

Comments
 (0)