Clarify lifetimes of named_arg parameters (#1051)

* Clarify usage of fmt::arg

Document that fmt::arg takes a non-owning
reference, even if that reference is to
a temporary. As such, users should make sure
the lifetime of the reference lasts as long
as the named argument.

* Clean up language

Remove mentions of `std::reference_wrapper` and rvalues
in favor of more common terminology like dangling references.
This commit is contained in:
Michael Lui 2019-02-22 11:37:19 -05:00 committed by Victor Zverovich
parent 4e5694fd05
commit 187bd1b8b2

View File

@ -1200,6 +1200,7 @@ const unsigned long long format_arg_store<Context, Args...>::TYPES =
Constructs an `~fmt::format_arg_store` object that contains references to Constructs an `~fmt::format_arg_store` object that contains references to
arguments and can be implicitly converted to `~fmt::format_args`. `Context` arguments and can be implicitly converted to `~fmt::format_args`. `Context`
can be omitted in which case it defaults to `~fmt::context`. can be omitted in which case it defaults to `~fmt::context`.
See `~fmt::arg` for lifetime considerations.
\endrst \endrst
*/ */
template <typename Context = format_context, typename... Args> template <typename Context = format_context, typename... Args>
@ -1384,6 +1385,12 @@ typename buffer_context<Char>::type::iterator vformat_to(
\rst \rst
Returns a named argument to be used in a formatting function. Returns a named argument to be used in a formatting function.
The named argument holds a reference and does not extend the lifetime
of its arguments.
Consequently, a dangling reference can accidentally be created.
The user should take care to only pass this function temporaries when
the named argument is itself a temporary, as per the following example.
**Example**:: **Example**::
fmt::print("Elapsed time: {s:.2f} seconds", fmt::arg("s", 1.23)); fmt::print("Elapsed time: {s:.2f} seconds", fmt::arg("s", 1.23));