std::format & std::print (C++20/23)
Prev: io_and_filesystem | Up: io_and_filesystem | Next: std::filesystem::path
Rel: std::iostream & std::fstream, std::string
Ref: cppreference — std::format | cppreference — std::print
Tags: #public #prog #cpp #stdlib #io #tier/core
Overview
std::format (<format>, C++20) and std::print / std::println (<print>, C++23) bring compile-time-checked, Python-style {} formatting to C++—faster, cleaner, and far less stateful than iostream manipulators (std::setw, std::hex) and safer than printf.
Notes & Pitfalls
- Type-safe, compile-time checked formatting (C++20
std::format, C++23std::print): Faster, safer, and cleaner than bothprintfandiostreammanipulators.
Example
#include <cassert>
#include <format>
#include <string>
int main() {
std::string msg = std::format("{} = {:.2f}", "pi", 3.14159);
assert(msg == "pi = 3.14");
}
Linked from
Related notes
- tuts/cpp/stdlib/io_and_filesystem/iostream Linked here; links to this page; shared tags: #cpp, #io, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/string Linked here; links to this page; shared tags: #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/io_and_filesystem Linked here; links to this page; shared tags: #cpp, #io, #prog, #stdlib
- tuts/cpp/stdlib/io_and_filesystem/filesystem_path Linked here; links to this page; shared tags: #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/algorithm_and_ranges/find_if shared tags: #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/algorithm_and_ranges/ranges_views shared tags: #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/algorithm_and_ranges/sort shared tags: #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/algorithm_and_ranges/transform shared tags: #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/concurrency/atomic shared tags: #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/concurrency/jthread shared tags: #cpp, #prog, #stdlib, #tier