fmt
Fast, safe, Python-style string formatting — the basis of C++20's `std::format`.
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-devGetting started
The smallest useful thing you can do with it, and what each part means.
#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;
}#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;
}Advanced usage
Where the library earns its place over a simpler alternative.
#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;
}#include <fmt/core.h>
#include <iostream>
int main() {
std::cout << fmt::format("|{:<10}|{:^10}|{:>10}|", "left", "center", "right") << std::endl;
return 0;
}#include <fmt/core.h>
#include <fstream>
int main() {
std::ofstream file("output.txt");
fmt::print(file, "Hello {}!\n", "World");
return 0;
}#include <fmt/compile.h>
#include <iostream>
int main() {
std::cout << fmt::format(FMT_COMPILE("Hello {}!"), "World") << std::endl;
return 0;
}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.
std::format
Standardised in C++20, based directly on this library
spdlog
Logging built on fmt, if formatting is for log output
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.
