fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
19.84k stars 2.42k forks source link

Fix format_as for non-const begin/end views #3955

Closed Arghnews closed 1 month ago

Arghnews commented 1 month ago

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 https://github.com/fmtlib/fmt/blob/1dc71f21eadac50c85b5c840ca7db2bf7a507b15/include/fmt/ranges.h#L485-L490 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 https://github.com/fmtlib/fmt/blob/1dc71f21eadac50c85b5c840ca7db2bf7a507b15/include/fmt/ranges.h#L541-L545 However other control paths (that pass lvalues) then flow through this, which doesn't seem right, so didn't opt for that method

vitaut commented 1 month ago

Merged, thanks!