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

Add xchar support for fmt::streamed(). #2961

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {

using ostream_formatter = basic_ostream_formatter<char>;

template <typename T>
struct formatter<detail::streamed_view<T>> : ostream_formatter {
template <typename T, typename Char>
struct formatter<detail::streamed_view<T>, Char>
: basic_ostream_formatter<Char> {
template <typename OutputIt>
auto format(detail::streamed_view<T> view,
basic_format_context<OutputIt, char>& ctx) const -> OutputIt {
return ostream_formatter::format(view.value, ctx);
basic_format_context<OutputIt, Char>& ctx) const -> OutputIt {
return basic_ostream_formatter<Char>::format(view.value, ctx);
}
};

Expand Down
13 changes: 13 additions & 0 deletions test/xchar-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ TEST(xchar_test, enum) {
EXPECT_EQ(L"0", fmt::format(L"{}", unstreamable_enum()));
}

struct streamable_and_unformattable {};

auto operator<<(std::wostream& os, streamable_and_unformattable)
-> std::wostream& {
return os << L"foo";
}

TEST(xchar_test, streamed) {
EXPECT_FALSE(fmt::is_formattable<streamable_and_unformattable>());
EXPECT_EQ(fmt::format(L"{}", fmt::streamed(streamable_and_unformattable())),
L"foo");
}

TEST(xchar_test, sign_not_truncated) {
wchar_t format_str[] = {
L'{', L':',
Expand Down