Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -3286,10 +3286,18 @@ 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});
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_formatter<typename _FormatContext::iterator, _CharT>{
._Ctx = _STD addressof(_FormatCtx), ._Specs = _STD addressof(_Format_specs)},
basic_format_arg<_FormatContext>{_Val});
} else {
return _STD visit_format_arg(
_Arg_formatter<typename _FormatContext::iterator, _CharT>{
._Ctx = _STD addressof(_FormatCtx), ._Specs = _STD addressof(_Format_specs)},
Comment thread
StephanTLavavej marked this conversation as resolved.
Outdated
basic_format_arg<_FormatContext>{static_cast<_Storage_type>(_Val)});
}
}

private:
Expand Down
21 changes: 21 additions & 0 deletions tests/std/tests/P0645R10_text_formatting_formatting/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,27 @@ void test() {
test_localized_char<wchar_t, wchar_t>();
}

template <class T>
struct Box {
T value;
};

template <class T, class CharT>
struct std::formatter<Box<T>, CharT> : std::formatter<T, CharT> {
template <class FormatContext>
auto format(Box<T> t, FormatContext& fc) const {
return formatter<T, CharT>::format(t.value, fc);
}
};

// Also test GH-2765 "<format>: Cannot format a long value with formatter"
void test_gh_2765() {
Comment thread
StephanTLavavej marked this conversation as resolved.
Outdated
Box<long> v = {42};
Comment thread
StephanTLavavej marked this conversation as resolved.
Outdated
assert(format("{:#x}", v) == "0x2a"s);
}

int main() {
test();

test_gh_2765();
}