Header Reference: <format>
Header Reference: <format>
The formatting API centered on std::format and formatter customizations.
How to use this reference page
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
- Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
- Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
- Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.
What header pages are for
Header reference pages are meant to answer a practical question quickly: what this header provides, when to reach for it, and which usage rules are easiest to get wrong.
- Start here when you already know roughly which header you need but want a fast operational summary.
- Use the example section below as a minimal pattern, then adapt it to your real container, ownership, text, or concurrency workflow.
- Jump to broader index pages when you need exhaustive coverage rather than a header-focused summary.
Main facilities
std::format
std::vformat
- formatters and formatter customization points
What it provides
- type-safe formatted string construction
- Python-style replacement fields and formatting syntax
- a modern alternative to ad hoc stream chains for complex formatting
When to use it
- when formatting logic is more complex than a simple stream insertion chain
- when you want clearer control over alignment, precision, and formatting style
Small worked example
#include <format>
#include <string>
int main() {
std::string line = std::format("{:>8} {:.2f}", "price", 12.5);
return static_cast<int>(line.size());
}
This is the core <format> pattern: format into a string with explicit width, precision, and layout rules instead of growing a stream chain by hand.
Selection guide
- use
<format> when formatting is nontrivial or the result must be stored, returned, or composed further
- use
<print> when the formatted text should go directly to output
- prefer formatter customization when the same domain type must be rendered consistently across many call sites
Nearby pages
Minimal example
#include <format>
#include <string>
int main() {
std::string line = std::format("value = {}", 42);
return static_cast<int>(line.size());
}
What to verify before relying on this header
- Whether the code needs ownership, borrowing, locale awareness, or low-allocation conversion behavior.
- How errors are reported: stream state, parse result, exception, error code, or boolean match result.
- Whether encoding, formatting, or lifetime assumptions need to be made explicit in the surrounding API.