Skip to content

Commit 27e67cd

Browse files
H-G-HristovZingam
andauthored
Reland: [libc++][format] P2637R3: Member visit (std::basic_format_arg) #76449 (#79032)
Deleted the offending test case. `libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp` lines: 134-135: > test<Context, bool, long>(true, 192812079084L); test<Context, bool, long>(false, 192812079084L); Relands: #76449 Reverted in: 02f95b7 --------- Co-authored-by: Zingam <[email protected]>
1 parent 8d89dd4 commit 27e67cd

File tree

15 files changed

+924
-49
lines changed

15 files changed

+924
-49
lines changed

libcxx/docs/ReleaseNotes/19.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ see the `releases page <https://llvm.org/releases/>`_.
3535
What's New in Libc++ 19.0.0?
3636
==============================
3737

38-
TODO
38+
- P2637R3 - Member ``visit``
3939

4040

4141
Implemented Papers

libcxx/docs/Status/Cxx2cPapers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"`P0792R14 <https://wg21.link/P0792R14>`__","LWG","``function_ref``: a type-erased callable reference","Varna June 2023","","",""
1818
"`P2874R2 <https://wg21.link/P2874R2>`__","LWG","Mandating Annex D Require No More","Varna June 2023","","",""
1919
"`P2757R3 <https://wg21.link/P2757R3>`__","LWG","Type-checking format args","Varna June 2023","","","|format|"
20-
"`P2637R3 <https://wg21.link/P2637R3>`__","LWG","Member ``visit``","Varna June 2023","|Partial|","18.0",""
20+
"`P2637R3 <https://wg21.link/P2637R3>`__","LWG","Member ``visit``","Varna June 2023","|Complete|","19.0",""
2121
"`P2641R4 <https://wg21.link/P2641R4>`__","CWG, LWG","Checking if a ``union`` alternative is active","Varna June 2023","","",""
2222
"`P1759R6 <https://wg21.link/P1759R6>`__","LWG","Native handles and file streams","Varna June 2023","|Complete|","18.0",""
2323
"`P2697R1 <https://wg21.link/P2697R1>`__","LWG","Interfacing ``bitset`` with ``string_view``","Varna June 2023","|Complete|","18.0",""

libcxx/docs/Status/FormatIssues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Number,Name,Standard,Assignee,Status,First released version
1616
"`P2693R1 <https://wg21.link/P2693R1>`__","Formatting ``thread::id`` and ``stacktrace``","C++23","Mark de Wever","|In Progress|"
1717
"`P2510R3 <https://wg21.link/P2510R3>`__","Formatting pointers","C++26","Mark de Wever","|Complete|",17.0
1818
"`P2757R3 <https://wg21.link/P2757R3>`__","Type-checking format args","C++26","","",
19-
"`P2637R3 <https://wg21.link/P2637R3>`__","Member ``visit``","C++26","","",
19+
"`P2637R3 <https://wg21.link/P2637R3>`__","Member ``visit``","C++26","Hristo Hristov","|Complete|",19.0
2020
"`P2905R2 <https://wg21.link/P2905R2>`__","Runtime format strings","C++26 DR","Mark de Wever","|Complete|",18.0
2121
"`P2918R2 <https://wg21.link/P2918R2>`__","Runtime format strings II","C++26","Mark de Wever","|Complete|",18.0
2222
"`P2909R4 <https://wg21.link/P2909R4>`__","Fix formatting of code units as integers (Dude, where’s my ``char``?)","C++26 DR","Mark de Wever","|Complete|",18.0

libcxx/include/__config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,12 @@ typedef __char32_t char32_t;
10151015
# define _LIBCPP_DEPRECATED_IN_CXX23
10161016
# endif
10171017

1018+
# if _LIBCPP_STD_VER >= 26
1019+
# define _LIBCPP_DEPRECATED_IN_CXX26 _LIBCPP_DEPRECATED
1020+
# else
1021+
# define _LIBCPP_DEPRECATED_IN_CXX26
1022+
# endif
1023+
10181024
# if !defined(_LIBCPP_HAS_NO_CHAR8_T)
10191025
# define _LIBCPP_DEPRECATED_WITH_CHAR8_T _LIBCPP_DEPRECATED
10201026
# else

libcxx/include/__format/format_arg.h

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr __arg_t __get_packed_type(uint64_t __types, size
9696

9797
} // namespace __format
9898

99-
// This function is not user obervable, so it can directly use the non-standard
99+
// This function is not user observable, so it can directly use the non-standard
100100
// types of the "variant". See __arg_t for more details.
101101
template <class _Visitor, class _Context>
102102
_LIBCPP_HIDE_FROM_ABI decltype(auto) __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
@@ -147,6 +147,59 @@ _LIBCPP_HIDE_FROM_ABI decltype(auto) __visit_format_arg(_Visitor&& __vis, basic_
147147
__libcpp_unreachable();
148148
}
149149

150+
# if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
151+
152+
template <class _Rp, class _Visitor, class _Context>
153+
_LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
154+
switch (__arg.__type_) {
155+
case __format::__arg_t::__none:
156+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__monostate_);
157+
case __format::__arg_t::__boolean:
158+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__boolean_);
159+
case __format::__arg_t::__char_type:
160+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__char_type_);
161+
case __format::__arg_t::__int:
162+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__int_);
163+
case __format::__arg_t::__long_long:
164+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__long_long_);
165+
case __format::__arg_t::__i128:
166+
# ifndef _LIBCPP_HAS_NO_INT128
167+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__i128_);
168+
# else
169+
__libcpp_unreachable();
170+
# endif
171+
case __format::__arg_t::__unsigned:
172+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_);
173+
case __format::__arg_t::__unsigned_long_long:
174+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_long_long_);
175+
case __format::__arg_t::__u128:
176+
# ifndef _LIBCPP_HAS_NO_INT128
177+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__u128_);
178+
# else
179+
__libcpp_unreachable();
180+
# endif
181+
case __format::__arg_t::__float:
182+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__float_);
183+
case __format::__arg_t::__double:
184+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__double_);
185+
case __format::__arg_t::__long_double:
186+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__long_double_);
187+
case __format::__arg_t::__const_char_type_ptr:
188+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__const_char_type_ptr_);
189+
case __format::__arg_t::__string_view:
190+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__string_view_);
191+
case __format::__arg_t::__ptr:
192+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__ptr_);
193+
case __format::__arg_t::__handle:
194+
return std::invoke_r<_Rp>(
195+
std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__arg.__value_.__handle_});
196+
}
197+
198+
__libcpp_unreachable();
199+
}
200+
201+
# endif // _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
202+
150203
/// Contains the values used in basic_format_arg.
151204
///
152205
/// This is a separate type so it's possible to store the values and types in
@@ -230,6 +283,52 @@ class _LIBCPP_TEMPLATE_VIS basic_format_arg {
230283

231284
_LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { return __type_ != __format::__arg_t::__none; }
232285

286+
# if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
287+
288+
// This function is user facing, so it must wrap the non-standard types of
289+
// the "variant" in a handle to stay conforming. See __arg_t for more details.
290+
template <class _Visitor>
291+
_LIBCPP_HIDE_FROM_ABI decltype(auto) visit(this basic_format_arg __arg, _Visitor&& __vis) {
292+
switch (__arg.__type_) {
293+
# ifndef _LIBCPP_HAS_NO_INT128
294+
case __format::__arg_t::__i128: {
295+
typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__i128_};
296+
return std::invoke(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
297+
}
298+
299+
case __format::__arg_t::__u128: {
300+
typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__u128_};
301+
return std::invoke(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
302+
}
303+
# endif
304+
default:
305+
return std::__visit_format_arg(std::forward<_Visitor>(__vis), __arg);
306+
}
307+
}
308+
309+
// This function is user facing, so it must wrap the non-standard types of
310+
// the "variant" in a handle to stay conforming. See __arg_t for more details.
311+
template <class _Rp, class _Visitor>
312+
_LIBCPP_HIDE_FROM_ABI _Rp visit(this basic_format_arg __arg, _Visitor&& __vis) {
313+
switch (__arg.__type_) {
314+
# ifndef _LIBCPP_HAS_NO_INT128
315+
case __format::__arg_t::__i128: {
316+
typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__i128_};
317+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
318+
}
319+
320+
case __format::__arg_t::__u128: {
321+
typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__u128_};
322+
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
323+
}
324+
# endif
325+
default:
326+
return std::__visit_format_arg<_Rp>(std::forward<_Visitor>(__vis), __arg);
327+
}
328+
}
329+
330+
# endif // _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
331+
233332
private:
234333
using char_type = typename _Context::char_type;
235334

@@ -270,7 +369,11 @@ class _LIBCPP_TEMPLATE_VIS basic_format_arg<_Context>::handle {
270369
// This function is user facing, so it must wrap the non-standard types of
271370
// the "variant" in a handle to stay conforming. See __arg_t for more details.
272371
template <class _Visitor, class _Context>
273-
_LIBCPP_HIDE_FROM_ABI decltype(auto) visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
372+
# if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
373+
_LIBCPP_DEPRECATED_IN_CXX26
374+
# endif
375+
_LIBCPP_HIDE_FROM_ABI decltype(auto)
376+
visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
274377
switch (__arg.__type_) {
275378
# ifndef _LIBCPP_HAS_NO_INT128
276379
case __format::__arg_t::__i128: {
@@ -282,7 +385,7 @@ _LIBCPP_HIDE_FROM_ABI decltype(auto) visit_format_arg(_Visitor&& __vis, basic_fo
282385
typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__u128_};
283386
return std::invoke(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
284387
}
285-
# endif
388+
# endif // _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
286389
default:
287390
return std::__visit_format_arg(std::forward<_Visitor>(__vis), __arg);
288391
}

libcxx/include/__format/format_context.h

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,25 @@ class _LIBCPP_TEMPLATE_VIS basic_format_context<typename __format::__retarget_bu
166166
# endif
167167
__ctx_(std::addressof(__ctx)),
168168
__arg_([](void* __c, size_t __id) {
169-
return std::visit_format_arg(
170-
[&](auto __arg) -> basic_format_arg<basic_format_context> {
171-
if constexpr (same_as<decltype(__arg), monostate>)
172-
return {};
173-
else if constexpr (same_as<decltype(__arg), typename basic_format_arg<_Context>::handle>)
174-
// At the moment it's not possible for formatting to use a re-targeted handle.
175-
// TODO FMT add this when support is needed.
176-
std::__throw_format_error("Re-targeting handle not supported");
177-
else
178-
return basic_format_arg<basic_format_context>{
179-
__format::__determine_arg_t<basic_format_context, decltype(__arg)>(),
180-
__basic_format_arg_value<basic_format_context>(__arg)};
181-
},
182-
static_cast<_Context*>(__c)->arg(__id));
169+
auto __visitor = [&](auto __arg) -> basic_format_arg<basic_format_context> {
170+
if constexpr (same_as<decltype(__arg), monostate>)
171+
return {};
172+
else if constexpr (same_as<decltype(__arg), typename basic_format_arg<_Context>::handle>)
173+
// At the moment it's not possible for formatting to use a re-targeted handle.
174+
// TODO FMT add this when support is needed.
175+
std::__throw_format_error("Re-targeting handle not supported");
176+
else
177+
return basic_format_arg<basic_format_context>{
178+
__format::__determine_arg_t<basic_format_context, decltype(__arg)>(),
179+
__basic_format_arg_value<basic_format_context>(__arg)};
180+
};
181+
# if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
182+
return static_cast<_Context*>(__c)->arg(__id).visit(std::move(__visitor));
183+
# else
184+
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
185+
return std::visit_format_arg(std::move(__visitor), static_cast<_Context*>(__c)->arg(__id));
186+
_LIBCPP_SUPPRESS_DEPRECATED_POP
187+
# endif // _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
183188
}) {
184189
}
185190

libcxx/include/format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ namespace std {
170170
template<class Context> class basic_format_arg;
171171
172172
template<class Visitor, class Context>
173-
see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
173+
see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg); // Deprecated in C++26
174174
175175
// [format.arg.store], class template format-arg-store
176176
template<class Context, class... Args> struct format-arg-store; // exposition only

0 commit comments

Comments
 (0)