How to use C++ std::ostream with printf-like formatting?

In C++20 you can use std::format for safe printf-like formatting:

std::cout << std::format("The answer is {}.\n", 42);

In addition to that the {fmt} library, std::format is based on, provides the print function that combines formatting and output:

fmt::print("The answer is {}.\n", 42);

Disclaimer: I’m the author of {fmt} and C++20 std::format.

Leave a Comment