All Formatter and Printing Customization Points
All Formatter and Printing Customization Points
A compact scan page for formatter specializations, format context types, and print-oriented customization hooks in modern C++.
All Formatter and Printing Customization Points
A compact scan page for formatter specializations, format context types, and print-oriented customization hooks in modern C++.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
std::formatter<T, CharT> specialization pointsstd::format_contextstd::wformat_contextstd::format_parse_contextstd::basic_format_contextstd::formatstd::vformatstd::format_tostd::formatted_sizestd::printstd::printlnparse() for format-spec parsingformat() for value emission into the format context#include <format>
#include <string_view>
struct Point {
int x{};
int y{};
};
template <>
struct std::formatter<Point> : std::formatter<std::string_view> {
auto format(const Point& point, std::format_context& ctx) const {
return std::format_to(ctx.out(), "({}, {})", point.x, point.y);
}
};
The main idea is simple: parse any custom specifiers you want in parse(), then write the formatted output in format(). If you do not need custom specifiers, keep parse() minimal and focus on stable readable output.
#include <format>
#include <string>
struct Point {
int x{};
int y{};
};
template <>
struct std::formatter<Point> : std::formatter<std::string_view> {
auto format(const Point& point, std::format_context& ctx) const {
return std::formatter<std::string_view>::format("Point", ctx);
}
};
int main() {
return std::format("{}", Point{3, 4}).size();
}