Fix format_as for non-const begin/end views - #3955
Merged
Merged
Conversation
vitaut
reviewed
May 12, 2024
| 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); |
Contributor
There was a problem hiding this comment.
Can it introduce an extra copy? Can val be a reference?
Contributor
Author
There was a problem hiding this comment.
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
force-pushed
the
format_as_std_filter_view_fix
branch
from
May 13, 2024 02:12
cb057a4 to
ae42fbd
Compare
Contributor
|
Merged, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes issue #3752
base::formatdoes not accept rvalues, using the result offormat_asdirectly means it is passed one. Doesn't matter for ranges with a const begin and end, but sincestd::ranges::filter_viewcaches the next value to return, it only has a mutable begin/end iterator.If we just store the result of
format_asin 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
And we don't call
std::forwardanyway, I don't think we're performing any pessimisation by making this changeAnother way to fix this is by providing a
&&overload offmt/include/fmt/ranges.h
Lines 541 to 545 in 1dc71f2
However other control paths (that pass lvalues) then flow through this, which doesn't seem right, so didn't opt for that method