std::format
is a feature introduced in C++20 that provides a type-safe and extensible way to format text. It's designed to be a safer, more robust replacement for older C-style formatting functions like printf
and C++ stream-based formatting. The std::format
function resides in the <format>
header.
The basic syntax of std::format
is straightforward:
#include <format>
#include <iostream>
int main() {
std::string message = std::format("Hello, {}!", "world");
std::cout << message << std::endl; // Output: Hello, world!
}
std::format
uses format specifiers within the format string to indicate how to format the arguments. The format specifiers are enclosed in curly braces {}
.
std::string formatted = std::format("The answer is {}.", 42);
You can specify the position of the arguments to be formatted:
std::string formatted = std::format("{1} before {0}", "world", "Hello");
// Output: "Hello before world"
You can specify the type of formatting you want for each argument:
Integers: You can specify the base, padding, etc.
std::string hex_format = std::format("Hex: {:x}", 255);
// Output:
// "Hex: ff"
Floating-Point Numbers: You can specify the precision, scientific notation, etc.
std::string float_format = std::format("Float: {:.2f}", 3.14159);
// Output:
// "Float: 3.14"
Strings: You can specify the alignment and width.
std::string string_format = std::format("String: {:>10}", "right");
// Output:
// "String: right"
One of the advantages of std::format
is that it's type-safe. If you try to use a format specifier that's incompatible with the type of the argument, it will result in a compile-time error.
std::format
is generally more efficient than using string streams for formatting, although it might not be as fast as printf
in some cases. However, the type safety and extensibility often make it a better choice.