@@ -2447,11 +2447,17 @@ public:
24472447 }
24482448};
24492449
2450- using _Fmt_it = back_insert_iterator<_Fmt_buffer<char>>;
2451- using _Fmt_wit = back_insert_iterator<_Fmt_buffer<wchar_t>>;
2450+ template <class _CharT>
2451+ using _Basic_fmt_it = back_insert_iterator<_Fmt_buffer<_CharT>>;
2452+
2453+ using _Fmt_it = _Basic_fmt_it<char>;
2454+ using _Fmt_wit = _Basic_fmt_it<wchar_t>;
2455+
2456+ template <class _CharT>
2457+ using _Default_format_context = basic_format_context<_Basic_fmt_it<_CharT>, _CharT>;
24522458
2453- _EXPORT_STD using format_context = basic_format_context<_Fmt_it, char>;
2454- _EXPORT_STD using wformat_context = basic_format_context<_Fmt_wit, wchar_t>;
2459+ _EXPORT_STD using format_context = _Default_format_context< char>;
2460+ _EXPORT_STD using wformat_context = _Default_format_context< wchar_t>;
24552461
24562462#if _HAS_CXX23
24572463_EXPORT_STD enum class range_format { disabled, map, set, sequence, string, debug_string };
@@ -4107,6 +4113,21 @@ private:
41074113
41084114 _Fill_align_and_width_specs<_CharT> _Specs;
41094115
4116+ template <class _FormatContext, class... _ArgTypes>
4117+ void _Format_to_context(_FormatContext& _Fmt_ctx, _ArgTypes&... _Args) const {
4118+ _STD _Copy_unchecked(_Opening_bracket._Unchecked_begin(), _Opening_bracket._Unchecked_end(), _Fmt_ctx.out());
4119+ [&]<size_t... _Indices>(index_sequence<_Indices...>) {
4120+ auto _Single_writer = [&]<size_t _Idx>(auto& _Arg) {
4121+ if constexpr (_Idx != 0) {
4122+ _STD _Copy_unchecked(_Separator._Unchecked_begin(), _Separator._Unchecked_end(), _Fmt_ctx.out());
4123+ }
4124+ _STD get<_Idx>(_Underlying).format(_Arg, _Fmt_ctx);
4125+ };
4126+ (_Single_writer.template operator()<_Indices>(_Args), ...);
4127+ }(index_sequence_for<_ArgTypes...>{});
4128+ _STD _Copy_unchecked(_Closing_bracket._Unchecked_begin(), _Closing_bracket._Unchecked_end(), _Fmt_ctx.out());
4129+ }
4130+
41104131protected:
41114132 static constexpr bool _Is_const_formattable = (formattable<const _Types, _CharT> && ...);
41124133
@@ -4121,26 +4142,26 @@ protected:
41214142 _STD _Get_dynamic_specs<_Width_checker>(_Fmt_ctx.arg(static_cast<size_t>(_Specs._Dynamic_width_index)));
41224143 }
41234144
4124- basic_string<_CharT> _Tmp_buf;
4125- auto _Tmp_ctx = basic_format_context<back_insert_iterator<basic_string<_CharT>>, _CharT>::_Make_from(
4126- _STD back_inserter(_Tmp_buf), {}, _Fmt_ctx._Get_lazy_locale());
4145+ if (_Format_specs._Width <= 0) {
4146+ _Format_to_context(_Fmt_ctx, _Args...);
4147+ return _Fmt_ctx.out();
4148+ }
41274149
4128- _STD _Copy_unchecked(_Opening_bracket._Unchecked_begin(), _Opening_bracket._Unchecked_end(), _Tmp_ctx.out());
4129- [&]<size_t... _Indices>(index_sequence<_Indices...>) {
4130- auto _Single_writer = [&]<size_t _Idx>(auto& _Arg) {
4131- if constexpr (_Idx != 0) {
4132- _STD _Copy_unchecked(_Separator._Unchecked_begin(), _Separator._Unchecked_end(), _Tmp_ctx.out());
4133- }
4134- _STD get<_Idx>(_Underlying).format(_Arg, _Tmp_ctx);
4135- };
4136- (_Single_writer.template operator()<_Indices>(_Args), ...);
4137- }(index_sequence_for<_ArgTypes...>{});
4138- _STD _Copy_unchecked(_Closing_bracket._Unchecked_begin(), _Closing_bracket._Unchecked_end(), _Tmp_ctx.out());
4150+ basic_string<_CharT> _Tmp_str;
4151+ if constexpr (is_same_v<_FormatContext, _Default_format_context<_CharT>>) {
4152+ _Fmt_iterator_buffer<back_insert_iterator<basic_string<_CharT>>, _CharT> _Tmp_buf{
4153+ back_insert_iterator{_Tmp_str}};
4154+ auto _Tmp_ctx = _FormatContext::_Make_from(
4155+ _Basic_fmt_it<_CharT>{_Tmp_buf}, _Fmt_ctx._Get_args(), _Fmt_ctx._Get_lazy_locale());
4156+ _Format_to_context(_Tmp_ctx, _Args...);
4157+ } else {
4158+ _CSTD abort(); // no basic_format_context object other than _Default_format_context can be created
4159+ }
41394160
4140- const int _Width = _Measure_display_width<_CharT>(_Tmp_buf );
4161+ const int _Width = _Measure_display_width<_CharT>(_Tmp_str );
41414162 return _STD _Write_aligned(
41424163 _Fmt_ctx.out(), _Width, _Format_specs, _Fmt_align::_Left, [&](typename _FormatContext::iterator _Out) {
4143- return _STD _Fmt_write(_STD move(_Out), basic_string_view<_CharT>{_Tmp_buf });
4164+ return _STD _Fmt_write(_STD move(_Out), basic_string_view<_CharT>{_Tmp_str });
41444165 });
41454166 }
41464167
0 commit comments