Skip to content

Remove double forward #1387

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

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions strings/base_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,18 @@ namespace winrt::impl
template <typename... Args>
inline hstring base_format(Args&&... args)
{
auto const size = std::formatted_size(std::forward<Args>(args)...);
// don't forward because an object could be moved-from, causing issues
// for the second format call.
// not forwarding lets us take both rvalues and lvalues but pass them
// further down as an lvalue ref. some types can only be formatted
// when non-const (e.g. ranges::filter_view) so take a const reference
// as parameter wouldn't work for all scenarios.
auto const size = std::formatted_size(args...);
WINRT_ASSERT(size < UINT_MAX);
auto const size32 = static_cast<uint32_t>(size);

hstring_builder builder(size32);
WINRT_VERIFY_(size32, std::format_to_n(builder.data(), size32, std::forward<Args>(args)...).size);
WINRT_VERIFY_(size32, std::format_to_n(builder.data(), size32, args...).size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like you could still forward here. Although I guess without the first forward there's not much point anymore...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While formatters don't care about value category, apparently std::format_string does. So forwarding in one place but not the other would break it (like it currently is).

return builder.to_hstring();
}
#endif
Expand Down