Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix format a long value with formatter #2768

Merged
merged 9 commits into from
Jun 12, 2022
13 changes: 9 additions & 4 deletions stl/inc/format
Original file line number Diff line number Diff line change
@@ -3286,10 +3286,15 @@ struct _Formatter_base {
_FormatCtx.arg(static_cast<size_t>(_Specs._Dynamic_precision_index)));
}

return _STD visit_format_arg(
_Arg_formatter<typename _FormatContext::iterator, _CharT>{
._Ctx = _STD addressof(_FormatCtx), ._Specs = _STD addressof(_Format_specs)},
basic_format_arg<_FormatContext>{_Val});
_Arg_formatter<typename _FormatContext::iterator, _CharT> _Arg_fmt{
._Ctx = _STD addressof(_FormatCtx), ._Specs = _STD addressof(_Format_specs)};

using _Storage_type = typename _Format_arg_traits<_FormatContext>::template _Storage_type<_Ty>;
if constexpr (is_same_v<typename basic_format_arg<_FormatContext>::handle, _Storage_type>) {
return _STD visit_format_arg(_Arg_fmt, basic_format_arg<_FormatContext>{_Val});
} else {
return _STD visit_format_arg(_Arg_fmt, basic_format_arg<_FormatContext>{static_cast<_Storage_type>(_Val)});
}
}

private:
Original file line number Diff line number Diff line change
@@ -172,29 +172,35 @@ void test_format_family_overloads() {
template <class charT>
void test_custom_formattable_type() {
test_numeric_custom_formattable_type<int, charT>();
test_numeric_custom_formattable_type<long, charT>();
test_numeric_custom_formattable_type<long long, charT>();
test_numeric_custom_formattable_type<unsigned int, charT>();
test_numeric_custom_formattable_type<unsigned long, charT>();
test_numeric_custom_formattable_type<unsigned long long, charT>();
test_numeric_custom_formattable_type<short, charT>();
#ifdef _NATIVE_WCHAR_T_DEFINED
test_numeric_custom_formattable_type<unsigned short, charT>();
#endif
test_numeric_custom_formattable_type<float, charT>();
test_numeric_custom_formattable_type<double, charT>();
test_numeric_custom_formattable_type<long double, charT>();
}

template <class charT>
void test_mixed_custom_formattable_type() {
test_numeric_mixed_args_custom_formattable_type<int, charT>();
test_numeric_mixed_args_custom_formattable_type<long, charT>();
test_numeric_mixed_args_custom_formattable_type<long long, charT>();
test_numeric_mixed_args_custom_formattable_type<unsigned int, charT>();
test_numeric_mixed_args_custom_formattable_type<unsigned long, charT>();
test_numeric_mixed_args_custom_formattable_type<unsigned long long, charT>();
test_numeric_mixed_args_custom_formattable_type<short, charT>();
#ifdef _NATIVE_WCHAR_T_DEFINED
test_numeric_mixed_args_custom_formattable_type<unsigned short, charT>();
#endif
test_numeric_mixed_args_custom_formattable_type<float, charT>();
test_numeric_mixed_args_custom_formattable_type<double, charT>();
test_numeric_mixed_args_custom_formattable_type<long double, charT>();
}

int main() {