template<typename
Context>
class fmt::DynamicFormatArgStore< Context >
A dynamic list of formatting arguments with storage.
Use this when the arguments are not known until run time and so cannot be passed as a parameter pack. It can be implicitly converted into fmt::basic_format_args for passing into type-erased formatting functions such as fmt::vformat.
Arguments are added with push_back. Custom types and owning string types are copied into the store, which may allocate; built-in types and string views are stored by value. std::reference_wrapper suppresses the copy, in which case the referent must outlive the store.
Example:
auto store = fmt::DynamicFormatArgStore<fmt::FormatContext>();
store.push_back(42);
store.push_back("abc");
std::string result = fmt::vformat("{} and {}", store);
- Template Parameters
-
| Context | the formatting context the arguments are built for, for example fmt::FormatContext. |
Adds a named argument, which a format string can refer to by name rather than by position.
The name is always copied into the store, so it need not outlive this call. The value follows the same rules as the other overloads: wrap it in std::cref to store a reference instead of a copy.
- Parameters
-
Example:
auto store =
fmt::DynamicFormatArgStore<fmt::FormatContext>(); store.push_back(fmt::arg("name", "World")); auto s = fmt::vformat("Hello, {name}!", store);
- Exceptions
-
| std::bad_alloc | if storing the name or value fails. |
Adds an argument to the store.
Built-in types and string views are stored by value. Custom types and owning string types are copied into the store, allocating if necessary, so the original need not outlive the store.
- Parameters
-
| arg | the value to format later. |
Example:
auto store =
fmt::DynamicFormatArgStore<fmt::FormatContext>(); store.push_back(42); store.push_back("abc"); store.push_back(1.5f); auto s = fmt::vformat("{} and {} and {}", store);
- Exceptions
-
| std::bad_alloc | if copying the argument into the store fails. |
- See also
- push_back(std::reference_wrapper<T>) to store a reference instead.
Adds a reference to an argument, avoiding a copy.
The referent must outlive every use of the store, and changes to it are visible to later formatting. Only types that would otherwise be copied may be passed this way; built-in types and string views are cheap to copy and are rejected by a static_assert.
- Parameters
-
| arg | a reference wrapper, from std::ref or std::cref. |
Example:
auto store =
fmt::DynamicFormatArgStore<fmt::FormatContext>(); char band[] = "Rolling Stones"; store.push_back(std::cref(band)); band[9] = 'c'; // Changing band affects the output. auto s = fmt::vformat("{}", store); // "Rolling Scones"
- See also
- push_back(const T&) which copies instead.