diff --git a/stl/inc/format b/stl/inc/format index c928871602a..1ff7beae52f 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2707,8 +2707,11 @@ struct _Arg_formatter { }; // The top level set of parsing "actions". -template +template struct _Format_handler { + using _OutputIt = back_insert_iterator<_Fmt_buffer<_CharT>>; + using _Context = basic_format_context<_OutputIt, _CharT>; + basic_format_parse_context<_CharT> _Parse_context; _Context _Ctx; @@ -2859,101 +2862,107 @@ _NODISCARD auto make_wformat_args(const _Args&... _Vals) { return _Format_arg_store{_Vals...}; } -template -using format_args_t = basic_format_args>; - template _OutputIt> -_OutputIt vformat_to( - _OutputIt _Out, const string_view _Fmt, const format_args_t, char> _Args) { - _Format_handler<_OutputIt, char, basic_format_context<_OutputIt, char>> _Handler(_STD move(_Out), _Fmt, _Args); - _Parse_format_string(_Fmt, _Handler); - return _Handler._Ctx.out(); +_OutputIt vformat_to(_OutputIt _Out, const string_view _Fmt, const format_args _Args) { + if constexpr (is_same_v<_OutputIt, _Fmt_it>) { + _Format_handler _Handler(_Out, _Fmt, _Args); + _Parse_format_string(_Fmt, _Handler); + return _Out; + } else { + _Fmt_iterator_buffer<_OutputIt, char> _Buf(_STD move(_Out)); + _Format_handler _Handler(_Fmt_it{_Buf}, _Fmt, _Args); + _Parse_format_string(_Fmt, _Handler); + return _Buf._Out(); + } } template _OutputIt> -_OutputIt vformat_to( - _OutputIt _Out, const wstring_view _Fmt, const format_args_t, wchar_t> _Args) { - _Format_handler<_OutputIt, wchar_t, basic_format_context<_OutputIt, wchar_t>> _Handler( - _STD move(_Out), _Fmt, _Args); - _Parse_format_string(_Fmt, _Handler); - return _Handler._Ctx.out(); +_OutputIt vformat_to(_OutputIt _Out, const wstring_view _Fmt, const wformat_args _Args) { + if constexpr (is_same_v<_OutputIt, _Fmt_wit>) { + _Format_handler _Handler(_Out, _Fmt, _Args); + _Parse_format_string(_Fmt, _Handler); + return _Out; + } else { + _Fmt_iterator_buffer<_OutputIt, wchar_t> _Buf(_STD move(_Out)); + _Format_handler _Handler(_Fmt_wit{_Buf}, _Fmt, _Args); + _Parse_format_string(_Fmt, _Handler); + return _Buf._Out(); + } } template _OutputIt> -_OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const string_view _Fmt, - const format_args_t, char> _Args) { - _Format_handler<_OutputIt, char, basic_format_context<_OutputIt, char>> _Handler( - _STD move(_Out), _Fmt, _Args, _Lazy_locale{_Loc}); - _Parse_format_string(_Fmt, _Handler); - return _Handler._Ctx.out(); +_OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const string_view _Fmt, const format_args _Args) { + if constexpr (is_same_v<_OutputIt, _Fmt_it>) { + _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{_Loc}); + _Parse_format_string(_Fmt, _Handler); + return _Out; + } else { + _Fmt_iterator_buffer<_OutputIt, char> _Buf(_STD move(_Out)); + _Format_handler _Handler(_Fmt_it{_Buf}, _Fmt, _Args, _Lazy_locale{_Loc}); + _Parse_format_string(_Fmt, _Handler); + return _Buf._Out(); + } } template _OutputIt> -_OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const wstring_view _Fmt, - const format_args_t, wchar_t> _Args) { - _Format_handler<_OutputIt, wchar_t, basic_format_context<_OutputIt, wchar_t>> _Handler( - _STD move(_Out), _Fmt, _Args, _Lazy_locale{_Loc}); - _Parse_format_string(_Fmt, _Handler); - return _Handler._Ctx.out(); +_OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const wstring_view _Fmt, const wformat_args _Args) { + if constexpr (is_same_v<_OutputIt, _Fmt_wit>) { + _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{_Loc}); + _Parse_format_string(_Fmt, _Handler); + return _Out; + } else { + _Fmt_iterator_buffer<_OutputIt, wchar_t> _Buf(_STD move(_Out)); + _Format_handler _Handler(_Fmt_wit{_Buf}, _Fmt, _Args, _Lazy_locale{_Loc}); + _Parse_format_string(_Fmt, _Handler); + return _Buf._Out(); + } } template _OutputIt, class... _Types> _OutputIt format_to(_OutputIt _Out, const string_view _Fmt, const _Types&... _Args) { - _Fmt_iterator_buffer<_OutputIt, char> _Buf(_STD move(_Out)); - _STD vformat_to(_Fmt_it{_Buf}, _Fmt, _STD make_format_args(_Args...)); - return _Buf._Out(); + return _STD vformat_to(_STD move(_Out), _Fmt, _STD make_format_args(_Args...)); } template _OutputIt, class... _Types> _OutputIt format_to(_OutputIt _Out, const wstring_view _Fmt, const _Types&... _Args) { - _Fmt_iterator_buffer<_OutputIt, wchar_t> _Buf(_STD move(_Out)); - _STD vformat_to(_Fmt_wit{_Buf}, _Fmt, _STD make_wformat_args(_Args...)); - return _Buf._Out(); + return _STD vformat_to(_STD move(_Out), _Fmt, _STD make_wformat_args(_Args...)); } template _OutputIt, class... _Types> _OutputIt format_to(_OutputIt _Out, const locale& _Loc, const string_view _Fmt, const _Types&... _Args) { - _Fmt_iterator_buffer<_OutputIt, char> _Buf(_STD move(_Out)); - _STD vformat_to(_Fmt_it{_Buf}, _Loc, _Fmt, _STD make_format_args(_Args...)); - return _Buf._Out(); + return _STD vformat_to(_STD move(_Out), _Loc, _Fmt, _STD make_format_args(_Args...)); } template _OutputIt, class... _Types> _OutputIt format_to(_OutputIt _Out, const locale& _Loc, const wstring_view _Fmt, const _Types&... _Args) { - _Fmt_iterator_buffer<_OutputIt, wchar_t> _Buf(_STD move(_Out)); - _STD vformat_to(_Fmt_wit{_Buf}, _Loc, _Fmt, _STD make_wformat_args(_Args...)); - return _Buf._Out(); + return _STD vformat_to(_STD move(_Out), _Loc, _Fmt, _STD make_wformat_args(_Args...)); } _NODISCARD inline string vformat(const string_view _Fmt, const format_args _Args) { string _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _Fmt_iterator_buffer, char> _Buf(back_insert_iterator{_Str}); - _STD vformat_to(_Fmt_it{_Buf}, _Fmt, _Args); + _STD vformat_to(back_insert_iterator{_Str}, _Fmt, _Args); return _Str; } _NODISCARD inline wstring vformat(const wstring_view _Fmt, const wformat_args _Args) { wstring _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _Fmt_iterator_buffer, wchar_t> _Buf(back_insert_iterator{_Str}); - _STD vformat_to(_Fmt_wit{_Buf}, _Fmt, _Args); + _STD vformat_to(back_insert_iterator{_Str}, _Fmt, _Args); return _Str; } _NODISCARD inline string vformat(const locale& _Loc, const string_view _Fmt, const format_args _Args) { string _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _Fmt_iterator_buffer, char> _Buf(back_insert_iterator{_Str}); - _STD vformat_to(_Fmt_it{_Buf}, _Loc, _Fmt, _Args); + _STD vformat_to(back_insert_iterator{_Str}, _Loc, _Fmt, _Args); return _Str; } _NODISCARD inline wstring vformat(const locale& _Loc, const wstring_view _Fmt, const wformat_args _Args) { wstring _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _Fmt_iterator_buffer, wchar_t> _Buf(back_insert_iterator{_Str}); - _STD vformat_to(_Fmt_wit{_Buf}, _Loc, _Fmt, _Args); + _STD vformat_to(back_insert_iterator{_Str}, _Loc, _Fmt, _Args); return _Str; } diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index 0b899393263..0a409eb57a5 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -47,8 +47,11 @@ struct choose_literal { template auto make_testing_format_args(Args&&... vals) { - using context = basic_format_context>, charT>; - return make_format_args(forward(vals)...); + if constexpr (is_same_v) { + return make_wformat_args(forward(vals)...); + } else { + return make_format_args(forward(vals)...); + } } template