{fmt}: A Modern, Fast, and Safe Formatting Library for C++

Summary
{fmt} is a powerful open-source formatting library for C++ that offers a fast and type-safe alternative to traditional C stdio and C++ iostreams. It provides a modern API, implements C++20 std::format and C++23 std::print, and boasts superior performance and compile-time safety. With its extensive features and broad adoption, {fmt} is an excellent choice for modern C++ development.
Repository Info
Tags
Click on any tag to explore related repositories
Introduction
{fmt} is a highly acclaimed open-source formatting library for C++, designed to be a fast, safe, and modern alternative to printf and C++ iostreams. With over 22,898 stars on GitHub, it's a widely adopted solution for robust and efficient string formatting. The library offers a Python-like format string syntax, supports positional arguments for localization, and provides an implementation of the C++20 std::format and C++23 std::print proposals. It emphasizes performance, type-safety, and portability, making it an essential tool for contemporary C++ projects.
Installation
To integrate {fmt} into your project, you typically need to build it from source or include it as a header-only library. The recommended approach involves using a build system like CMake.
For detailed instructions on how to build the library from source, integrate it into your project, or use its header-only configuration, please refer to the official documentation: https://fmt.dev/latest/get-started/#building-from-source
Examples
{fmt} provides a versatile and intuitive API for various formatting tasks. Here are a few examples demonstrating its capabilities:
Print to stdout
#include <fmt/base.h>
int main() {
fmt::print("Hello, world!\n");
}
Format a string
std::string s = fmt::format("The answer is {}.", 42);
// s == "The answer is 42."
Format a string using positional arguments
std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy");
// s == "I'd rather be happy than right."
Print dates and times
#include <fmt/chrono.h>
#include <chrono>
int main() {
auto now = std::chrono::system_clock::now();
fmt::print("Date and time: {}\n", now);
fmt::print("Time: {:%H:%M}\n", now);
}
Print a container
#include <vector>
#include <fmt/ranges.h>
int main() {
std::vector<int> v = {1, 2, 3};
fmt::print("{}\n", v);
}
Why Use {fmt}?
{fmt} addresses several shortcomings of existing C++ formatting solutions, offering compelling advantages:
- Performance: It is significantly faster than
printf, C++ iostreams,to_string, andto_charsin many benchmarks, especially for floating-point and integer formatting. - Type-Safety: Unlike
printf,{fmt}is fully type-safe, preventing common errors like format string mismatches at compile time (with C++20). - Modern API: It offers a clean, intuitive API similar to Python's
str.format, supporting positional arguments crucial for internationalization. - C++20/C++23 Compatibility:
{fmt}provides an implementation ofstd::formatandstd::print, allowing for a smooth transition to future C++ standards. - Extensibility: Easily extendable to support user-defined types.
- Small Code Size: Minimal configuration requires only three files, leading to small compiled code size and fast compile times, comparable to
printf. - Portability: Consistent output across various platforms and support for older compilers.
- Reliability: Extensively tested and continuously fuzzed for robustness.
- Ease of Use: Self-contained, no external dependencies, and distributed under the permissive MIT license.
Many notable projects, including Apple's FoundationDB, Aseprite, Blizzard Battle.net, ClickHouse, MongoDB, PyTorch, and Windows Terminal, rely on {fmt} for their formatting needs, underscoring its reliability and efficiency.
Links
- GitHub Repository: https://github.com/fmtlib/fmt
- Official Documentation: https://fmt.dev
- StackOverflow Q&A: https://stackoverflow.com/questions/tagged/fmt