Skip to content

fmt

Fast, safe, Python-style string formatting — the basis of C++20's `std::format`.

Developer UtilitiesCLI/UtilsC++

What it is

fmt is a modern C++ library for safe and fast string formatting. It provides a type-safe alternative to printf and std::stringstream, supporting Python-like format strings and compile-time checks.

fmt allows formatting strings using Python-like replacement fields with `{}`. It supports compile-time checking, positional arguments, named arguments, width, alignment, precision, and localization.

Licence
MIT
Best known for
Being adopted into the standard as std::format

When to use it

The question documentation cannot answer for you — because it cannot recommend something else.

Reach for it when

  • Any C++ project that formats strings, especially before C++20 is available
  • You want compile-time format string checking rather than printf's runtime surprises

Look elsewhere when

  • You are on C++20 or later and `std::format` covers what you need

Installation

sudo apt install libfmt-dev

Getting started

The smallest useful thing you can do with it, and what each part means.

Basic string formatting
#include <fmt/core.h>
#include <iostream>

int main() {
    std::string name = "Alice";
    int age = 30;
    std::cout << fmt::format("{} is {} years old", name, age) << std::endl;
    return 0;
}
Formats a string using `{}` placeholders for variables.
Named arguments
#include <fmt/core.h>
#include <iostream>

int main() {
    std::cout << fmt::format("{name} is {age} years old", fmt::arg("name", "Bob"), fmt::arg("age", 25)) << std::endl;
    return 0;
}
Demonstrates using named arguments for string formatting.

Advanced usage

Where the library earns its place over a simpler alternative.

Formatting numbers
#include <fmt/core.h>
#include <iostream>

int main() {
    double pi = 3.14159265;
    std::cout << fmt::format("Pi rounded to 2 decimals: {:.2f}", pi) << std::endl;
    return 0;
}
Formats a floating-point number with 2 decimal places.
Alignment and width
#include <fmt/core.h>
#include <iostream>

int main() {
    std::cout << fmt::format("|{:<10}|{:^10}|{:>10}|", "left", "center", "right") << std::endl;
    return 0;
}
Shows left, center, and right alignment within a fixed width.
Printing to file
#include <fmt/core.h>
#include <fstream>

int main() {
    std::ofstream file("output.txt");
    fmt::print(file, "Hello {}!\n", "World");
    return 0;
}
Writes a formatted string directly to a file.
Compile-time formatting
#include <fmt/compile.h>
#include <iostream>

int main() {
    std::cout << fmt::format(FMT_COMPILE("Hello {}!"), "World") << std::endl;
    return 0;
}
Demonstrates compile-time checked formatting for better performance and safety.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

fmt::format_error
Thrown if the format string is invalid or arguments do not match placeholders. Ensure proper format specifiers and argument types.
std::out_of_range
Occurs if an argument index in the format string is out of range. Verify that placeholders match the number of provided arguments.

Best practices

  • Prefer fmt::format over printf for type safety.
  • Use named arguments for clarity in long format strings.
  • Leverage compile-time formatting for performance-critical code.
  • Use fmt::print for simple output to console or files.
  • Combine fmt with logging libraries to format log messages safely.

Alternatives

Comparable options, and the reason you would pick one over the other.

Background

Why it exists, and what it was reacting to.

fmt was created by Victor Zverovich to provide an efficient and safe formatting library for C++ that combines the convenience of Python’s str.format with C++ type safety. It became widely used and is the basis for C++20's std::format.