Introduction to the fmt library for C++
\n\nThe fmt library (or fmtlib) is a modern, fast, and safe library for string formatting in C++. It provides an alternative to outdated functions like printf and sprintf, as well as the cumbersome std::stringstream streams. The main goal of fmt is to make string formatting simple, type-safe, extensible, and performant.
Why is fmt needed? First, it solves the type safety problem. printf does not check argument types, which leads to undefined behavior and hard-to-find bugs. fmt, on the other hand, uses C++ templates and checks types at compile time. Second, fmt is significantly faster than std::stringstream, and often faster than printf. Third, its syntax is intuitive and resembles formatting in Python, making it easier to switch between languages. So much so that fmt became the basis for std::format, which was adopted into the C++20 standard.
On this site, pythonlib.ru, we often talk about Python, but fmt is an excellent example of how good ideas from Python migrated to the C++ world and became the de facto standard there. If you write in C++ and want to do it modernly, fmt is a must-have library in your arsenal.
\n\nInstalling the fmt library
\n\nInstallation via package managers (Conan, vcpkg)
\n\nThe easiest way to install fmt in a modern C++ project is to use a package manager.
\n\nConan:
\nconan install fmt/10.1.1@\n# or in your conanfile.txt:\n# [requires]\n# fmt/10.1.1\n\nvcpkg:
\nvcpkg install fmt\n# Then in CMakeLists.txt:\n# find_package(fmt CONFIG REQUIRED)\n# target_link_libraries(your_project PRIVATE fmt::fmt)\n\nManual installation (building from source)
\n\nYou can download the source code from GitHub and build it using CMake:
\ngit clone https://github.com/fmtlib/fmt.git\ncd fmt\nmkdir build && cd build\ncmake ..\nmake -j4\nsudo make install\n\nInstallation via pip (for use in Python extensions)
\n\nAlthough fmt is a C++ library, it can be installed as a Python package if you are writing a C++ extension using pybind11 or cppyy:
\npip install fmt\nHowever, in the context of pure C++, this does not make sense. Usually, you simply include the header files.
\n\nIntegrating into a project (header-only mode)
\n\nfmt can be used in header-only mode by simply copying the include/fmt folder into your project and adding:
#define FMT_HEADER_ONLY\n#include "fmt/core.h"\n#include "fmt/format.h"\n\nQuick start with a code example
\n\nLet's start with the simplest example that demonstrates basic formatting:
\n\n#include <fmt/core.h>\n#include <fmt/format.h>\n#include <iostream>\n\nint main() {\n // Simplest formatting\n std::string message = fmt::format("Hello, {}! You are {} years old.", "World", 42);\n std::cout << message << std::endl;\n\n // Output to stdout\n fmt::print("Pi is approximately {:.2f}\\n", 3.14159);\n\n // Formatting with named arguments (C++20 style)\n fmt::print("User: {name}, Age: {age}\\n",\n fmt::arg("name", "Alice"),\n fmt::arg("age", 30));\n\n return 0;\n}\n\nThis code will output:
\nHello, World! You are 42 years old.\nPi is approximately 3.14\nUser: Alice, Age: 30\n\nAs you can see, the {} syntax is very similar to Python. Everything is intuitive and readable.
Main methods and functions of the fmt library
\n\nBelow is a detailed breakdown of the key fmt functions. We will look at their signatures, parameters, and examples