On this page
  1. std::format & std::print (C++20/23)
  2. Overview
  3. Notes & Pitfalls
  4. Example

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

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