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_as for non-const begin/end views #3955

Merged
merged 1 commit into from
May 13, 2024

Conversation

Arghnews
Copy link
Contributor

Fixes issue #3752

base::format does not accept rvalues, using the result of format_as directly means it is passed one. Doesn't matter for ranges with a const begin and end, but since std::ranges::filter_view caches the next value to return, it only has a mutable begin/end iterator.

If we just store the result of format_as in a temporary variable, we can pass it as an lvalue.

Since we use it in

fmt/include/fmt/ranges.h

Lines 485 to 490 in 1dc71f2

template <typename R, typename FormatContext>
auto format(R&& range, FormatContext& ctx) const -> decltype(ctx.out()) {
auto mapper = detail::range_mapper<buffered_context<Char>>();
auto out = ctx.out();
auto it = detail::range_begin(range);
auto end = detail::range_end(range);

And we don't call std::forward anyway, I don't think we're performing any pessimisation by making this change


Another way to fix this is by providing a && overload of

fmt/include/fmt/ranges.h

Lines 541 to 545 in 1dc71f2

template <typename FormatContext>
auto format(range_type& range, FormatContext& ctx) const
-> decltype(ctx.out()) {
return range_formatter_.format(range, ctx);
}

However other control paths (that pass lvalues) then flow through this, which doesn't seem right, so didn't opt for that method

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

@@ -4000,7 +4000,9 @@ struct formatter<T, Char, enable_if_t<detail::has_format_as<T>::value>>
template <typename FormatContext>
auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) {
using base = formatter<detail::format_as_t<T>, Char>;
return base::format(format_as(value), ctx);
// format functions expect lvalue refs, not rvalues
auto val = format_as(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can it introduce an extra copy? Can val be a reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If format_as returns a local variable of type T by value then no extra copy will be made. This was my thought of the predominant use case.

However for a format_as function signature like: const std::string& format_as(T t). Let's say you have a lookup table of std::strings for T, then using auto val always copies, while auto&& val deduces the type of val as a const & so no copy is made.

https://godbolt.org/z/fqMrcY3c3

Thus I think your suggestion of using auto&& is a good one here! Will make the change

base::format does not accept rvalues, using the result of format_as
directly means it is passed one. Doesn't matter for ranges with a const
begin and end, but filter_view caches, so it only has mutable begin/end.
Use auto&& to avoid copy if format_as returns const ref
@Arghnews Arghnews force-pushed the format_as_std_filter_view_fix branch from cb057a4 to ae42fbd Compare May 13, 2024 02:12
@vitaut vitaut merged commit 8e72804 into fmtlib:master May 13, 2024
41 checks passed
@vitaut
Copy link
Contributor

vitaut commented May 13, 2024

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants