feat: Allow fmt::join to accept rvalue tuples #4554
+28
−16
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.
Summary
This PR updates
fmt::join
and its underlyingtuple_join_view
to accept rvalue tuples. This enhances usability and fixes a critical lifetime issue, particularly when usingfmt::join
withinformat_as
specializations.The Problem
Currently,
fmt::join
only accepts an lvalue reference to a tuple. This prevents passing a temporary tuple, such as one returned fromstd::make_tuple
orstd::tie
, directly to the function.This limitation is particularly dangerous when implementing a
format_as
overload. For example:In the code above, the
tuple_join_view
returned byfmt::join
holds a dangling reference to the temporary tuple created bystd::tie
, which is destroyed at the end of theformat_as
function. This leads to undefined behavior when the view is later used for formatting.The Solution
This PR resolves the issue by changing
fmt::join
to accept tuples via a forwarding reference (T&&
).tuple_join_view
, extending its lifetime and preventing dangling references.With this change, the
format_as
implementation shown above becomes safe and works as intuitively expected. This makesfmt::join
a much more powerful and safe tool for creating custom formatters for aggregate types.