Writes a formatted string to an arbitrary sink object (implementing the absl::FormatRawSink interface), using an UntypedFormatSpec and zero or more additional arguments.
Declared in <absl/strings/str_format.h>
[[nodiscard]]
bool
FormatUntyped(
FormatRawSink raw_sink,
UntypedFormatSpec const& format,
absl::Span<FormatArg const> args);
This function acts as the most generic formatting function in the str_format library. The caller provides a raw sink, an unchecked format string, and (usually) a runtime specified list of arguments; no compile-time checking of formatting is performed within this function. As a result, a caller should check the return value to verify that no error occurred. On failure, this function returns false and the state of the sink is unspecified.
The arguments are provided in an absl::Span<const absl::FormatArg>. Each absl::FormatArg object binds to a single argument and keeps a reference to it. The values used to create the FormatArg objects must outlive this function call.
Example:
std::optional<std::string> FormatDynamic( const std::string& in_format, const vector<std::string>& in_args) { std::string out; std::vector<absl::FormatArg> args; for (const auto& v : in_args) { // It is important that 'v' is a reference to the objects in in_args. // The values we pass to FormatArg must outlive the call to // FormatUntyped. args.emplace_back(v); } absl::UntypedFormatSpec format(in_format); if (!absl::FormatUntyped(&out, format, args)) { return std::nullopt; } return std::move(out); }
| Name | Description |
|---|---|
| raw_sink | The sink that receives the formatted output. |
| format | The untyped format spec. |
| args | The arguments substituted into the format string. |