Format your own type (Part 2) -- Sandor Dargo
Previously, we discussed how to write our own formatter and finished with a relatively simple solution for printing a struct called
ProgrammingLanguage. Today, we’ll take it to the next level.
Format your own type (Part 2)
by Sandor Dargo
From the article:
Add more options for semantic versioning
Let’s dream big. Instead of only handling major and minor versions (like Python 3.12), let’s aim to fully support semantic versioning.
Semantic versioning (SemVer) is a versioning scheme that conveys meaning about the underlying changes in a release. It typically consists of three parts: MAJOR.MINOR.PATCH.
We should be able to print all these correctly:
ProgrammingLanguage cpp{"C++", 20}; ProgrammingLanguage python312{"Python", 3, 12}; ProgrammingLanguage python31211{"Python", 3, 12, 11}; std::cout << std::format("{:%n%v} is fun", cpp) << '\n'; // C++20 is fun std::cout << std::format("{:%n %v} is fun", python312) << '\n'; // Python 3.12 is fun std::cout << std::format("{:%n %v} is fun", python31211) << '\n'; // Python 3.12.11 is fun


Registration is now open for CppCon 2025! The conference starts on September 13 and will be held