Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std::format

Use case

Type-safe printf-style formatting.

Explanation

std::format combines printf convenience with type safety.

Code

#include <cstdint>
#include <format>
#include <iostream>

int main() {
  uint64_t addr = 0x10001000;
  const char *name = "_main";

  std::string s = std::format("Symbol '{}' at {:#x}", name, addr);

  std::cout << s << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++20/build/std-format
Symbol '_main' at 0x10001000