fmt for C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to the fmt library for C++

\n\n

The 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.

\n\n

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.

\n\n

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\n

Installing the fmt library

\n\n

Installation via package managers (Conan, vcpkg)

\n\n

The easiest way to install fmt in a modern C++ project is to use a package manager.

\n\n

Conan:

\n
conan install fmt/10.1.1@\n# or in your conanfile.txt:\n# [requires]\n# fmt/10.1.1
\n\n

vcpkg:

\n
vcpkg install fmt\n# Then in CMakeLists.txt:\n# find_package(fmt CONFIG REQUIRED)\n# target_link_libraries(your_project PRIVATE fmt::fmt)
\n\n

Manual installation (building from source)

\n\n

You can download the source code from GitHub and build it using CMake:

\n
git clone https://github.com/fmtlib/fmt.git\ncd fmt\nmkdir build && cd build\ncmake ..\nmake -j4\nsudo make install
\n\n

Installation via pip (for use in Python extensions)

\n\n

Although fmt is a C++ library, it can be installed as a Python package if you are writing a C++ extension using pybind11 or cppyy:

\n
pip install fmt
\n

However, in the context of pure C++, this does not make sense. Usually, you simply include the header files.

\n\n

Integrating into a project (header-only mode)

\n\n

fmt can be used in header-only mode by simply copying the include/fmt folder into your project and adding:

\n
#define FMT_HEADER_ONLY\n#include "fmt/core.h"\n#include "fmt/format.h"
\n\n

Quick start with a code example

\n\n

Let'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\n

This code will output:

\n
Hello, World! You are 42 years old.\nPi is approximately 3.14\nUser: Alice, Age: 30
\n\n

As you can see, the {} syntax is very similar to Python. Everything is intuitive and readable.

\n\n

Main methods and functions of the fmt library

\n\n

Below is a detailed breakdown of the key fmt functions. We will look at their signatures, parameters, and examples

Recommendations