fmt
Loading...
Searching...
No Matches
fmt::DynamicFormatArgStore< Context > Class Template Referenceexport
module fmt

Public Types

using char_type = typename Context::char_type

Public Member Functions

constexpr DynamicFormatArgStore ()=default
 operator BasicFormatArgs< Context > () const
template<typename T>
auto push_back (const T &arg) -> void
template<typename T>
auto push_back (std::reference_wrapper< T > arg) -> void
template<typename T>
auto push_back (const NamedArg< T, char_type > &arg) -> void
auto clear () -> void
auto reserve (size_t new_cap, size_t new_cap_named) -> void
auto size () const noexcept -> size_t

Friends

class BasicFormatArgs< Context >

Detailed Description

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
Contextthe formatting context the arguments are built for, for example fmt::FormatContext.

Constructor & Destructor Documentation

◆ DynamicFormatArgStore()

template<typename Context>
fmt::DynamicFormatArgStore< Context >::DynamicFormatArgStore ( )
constexprdefault

Constructs a store holding no arguments.

Member Function Documentation

◆ clear()

template<typename Context>
auto fmt::DynamicFormatArgStore< Context >::clear ( ) -> void
inline

Removes all arguments and releases any memory they were copied into.

Any basic_format_args previously obtained from this store is left dangling.

◆ operator BasicFormatArgs< Context >()

template<typename Context>
fmt::DynamicFormatArgStore< Context >::operator BasicFormatArgs< Context > ( ) const
inline

Converts the store into a view of its arguments, for passing to a type-erased formatting function such as fmt::vformat.

Returns
a view referring to this store's arguments.
Warning
The view does not own the arguments. The store must outlive it, and must not be modified while it is in use: push_back, reserve and clear may all invalidate it.

◆ push_back() [1/3]

template<typename Context>
template<typename T>
auto fmt::DynamicFormatArgStore< Context >::push_back ( const NamedArg< T, char_type > & arg) -> void
inline

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
argan argument built by fmt::arg.

Example:

auto store =

fmt::DynamicFormatArgStore<fmt::FormatContext>(); store.push_back(fmt::arg("name", "World")); auto s = fmt::vformat("Hello, {name}!", store);

Exceptions
std::bad_allocif storing the name or value fails.

◆ push_back() [2/3]

template<typename Context>
template<typename T>
auto fmt::DynamicFormatArgStore< Context >::push_back ( const T & arg) -> void
inline

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
argthe 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_allocif copying the argument into the store fails.
See also
push_back(std::reference_wrapper<T>) to store a reference instead.

◆ push_back() [3/3]

template<typename Context>
template<typename T>
auto fmt::DynamicFormatArgStore< Context >::push_back ( std::reference_wrapper< T > arg) -> void
inline

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
arga 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.

◆ reserve()

template<typename Context>
auto fmt::DynamicFormatArgStore< Context >::reserve ( size_t new_cap,
size_t new_cap_named ) -> void
inline

Reserves capacity, so that subsequent push_back calls do not reallocate.

Parameters
new_capthe total number of arguments to make room for.
new_cap_namedhow many of those are named arguments.
Note
Reallocation invalidates any basic_format_args obtained from this store, so reserve before converting, not after.

◆ size()

template<typename Context>
auto fmt::DynamicFormatArgStore< Context >::size ( ) const -> size_t
inlinenoexcept

Returns the number of arguments in the store.

Returns
the count of arguments added with push_back, named and positional alike.

The documentation for this class was generated from the following file:
  • /home/mccakit/desktop/mccakit-fmt/fmt/src/fmt/iface/args.cppm