format/formattuples.cpp

The following code example is taken from the book
C++23 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2026
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <iostream>   // for std::cout
#include <format>     // for std::format
#include <print>      // for std::print
#include <utility>

int main ()
{
  std::tuple t{47, 0.4, "hi", '?'};    // 4-element tuple
  std::cout << std::format("{}\n", t);
  std::cout << std::format("{:_^25}\n", t);
  std::println("{:_^25}\n", t);

  std::println("default: {}", t);
  std::println("{{:n}} :   {:n}\n", t);

  std::pair p{"val", 11};             // could also be a 2-elem tuple
  std::println("default: {}", p);
  std::println("{{:n}} :   {:n}", p);
  std::println("{{:m}} :   {:m}\n", p);
}