A class template representing a preparsed FormatSpec, with template arguments specifying the conversion characters used within the format string.
Declared in <absl/strings/str_format.h>
template<auto Conv...>
using ParsedFormat = absl::str_format_internal::ExtendedParsedFormat<absl::str_format_internal::ToFormatConversionCharSet(Conv)...>;
Such characters must be valid format type specifiers, and these type specifiers are checked at compile-time.
Instances of ParsedFormat can be created, copied, and reused to speed up formatting loops. A ParsedFormat may either be constructed statically, or dynamically through its New() factory function, which only constructs a runtime object if the format is valid at that time.
Example:
// Verified at compile time. absl::ParsedFormat<'s', 'd'> format_string("Welcome to %s, Number %d!"); absl::StrFormat(format_string, "TheVillage", 6);
// Verified at runtime. auto format_runtime = absl::ParsedFormat<'d'>::New(format_string); if (format_runtime) { value = absl::StrFormat(*format_runtime, i); } else { ... error case ... }
An 'extended' format is also allowed that can specify multiple conversion characters per format argument, using a combination of absl::FormatConversionCharSet enum values (logically a set union) via the | operator. (Single character-based arguments are still accepted, but cannot be combined). Some common conversions also have predefined enum values, such as absl::FormatConversionCharSet::kIntegral.
Example: // Extended format supports multiple conversion characters per argument, // specified via a combination of FormatConversionCharSet enums. using MyFormat = absl::ParsedFormat<absl::FormatConversionCharSet::d | absl::FormatConversionCharSet::x>; MyFormat GetFormat(bool use_hex) { if (use_hex) return MyFormat("foo %x bar"); return MyFormat("foo %d bar"); } // format can be used with any value that supports 'd' and 'x', // like int. auto format = GetFormat(use_hex); value = StringF(format, i);