The following code example is taken from the book
C++20 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2021
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include "always42.hpp"
#include "formatalways42.hpp"
#include <iostream>
int main()
{
try {
Always42 val;
std::cout << val.getValue() << '\n';
std::cout << std::format("Value: {}\n", val);
std::cout << std::format("Twice: {0} {0}\n", val);
std::cout << std::format("With width: '{:7}'\n", val);
std::cout << std::format("With all: '{:.^7}'\n", val);
}
catch (std::format_error& e) {
std::cerr << "Format Error: " << e.what() << std::endl;
}
}