Favicon of fmt

fmt

Open-source C++ formatting library replacing printf and iostreams, with compile-time checks and high performance.

fmt website screenshot
fmt GitHub repository preview

{fmt} is a C++ formatting library that replaces C's printf and C++ iostreams with a safer, faster API using Python-like format string syntax. It's built for C++ developers who need fast, type-safe output formatting, whether for logging, CLI tools, file writing, or general string construction, and want to avoid the pitfalls of printf format specifiers or the verbosity of iostreams.

The library implements C++20 std::format and C++23 std::print, so code written against {fmt} maps closely to what's becoming standard library behavior. It has no external dependencies and ships as either a compiled library or a header-only configuration via the FMT_HEADER_ONLY macro.

Key features

  • Format API: a simple format function with positional arguments, useful for localization and reordering output without changing the underlying logic.
  • Standard library alignment: implements C++20 std::format and C++23 std::print so code is portable to newer standard library facilities.
  • Python-like syntax: format string syntax modeled on Python's str.format, familiar to developers coming from other languages.
  • Accurate floating-point formatting: uses the Dragonbox algorithm for IEEE 754 float formatting with correct rounding, shortness, and round-trip guarantees.
  • Unicode support: portable handling of Unicode text across platforms.
  • printf compatibility: a safe printf implementation, including the POSIX positional argument extension.
  • Extensibility: supports formatting user-defined types through a documented formatter interface.
  • Performance: benchmarks show it running faster than printf, iostreams, Boost Format, and Folly Format, and up to 20-30x faster than ostringstream/sprintf for float and double formatting.
  • Small footprint: minimum configuration is three files (base.h, format.h, format-inl.h), with compiled code size comparable to printf.
  • Compile-time safety: format string errors, such as invalid specifiers for a type, can be caught at compile time in C++20.
  • Color and style output: supports colored and styled terminal output with fg/bg colors and emphasis like bold, italic, underline.
  • Chrono and ranges support: dedicated headers for formatting dates, times, durations, and containers directly.
  • Locale independence by default: avoids locale-related surprises unless locale-aware formatting is explicitly requested.

Ideal use cases

{fmt} fits projects that need frequent string formatting or console/file output and want speed plus compile-time safety: logging libraries, game engines, databases, and server frameworks. It's a good replacement for printf-style code where you want type safety without giving up C-like format specifiers, and a good replacement for iostreams where compile times and code bloat matter. It's also useful as a stepping stone toward std::format/std::print for codebases not yet on a compiler with full C++20/23 support.

It is not the right choice if you're writing a project that has no formatting or text-output needs, or if your toolchain already provides std::format/std::print and you don't need {fmt}'s extras (color output, positional printf, chrono/ranges formatting) or broader compiler compatibility. If minimizing dependencies is a strict requirement and even a small header-only library is unacceptable, iostreams or C stdio remain the fallback.

Installation

{fmt} can be added to a project through CMake, as a header-only library, or via package managers. A minimal example using the compiled library:

#include <fmt/base.h>

int main() {
  fmt::print("Hello, world!\n");
}

To build and run tests from source, clone the repository and use CMake, per the "Building the library" instructions in the documentation. To run the benchmarks:

$ git clone --recursive https://github.com/fmtlib/format-benchmark.git
$ cd format-benchmark
$ cmake .
$ make speed-test
$ make bloat-test

For header-only use, define the FMT_HEADER_ONLY macro before including the headers, which avoids linking against a separate compiled library. Additional headers like fmt/chrono.h, fmt/ranges.h, fmt/os.h, and fmt/color.h enable formatting for dates/times, containers, file output, and colored terminal text respectively; include only what you need to keep compile times and binary size down. Full setup and integration details, including CMake package config, are in the official documentation at fmt.dev.

Categories:

Frequently asked questions

Share:

Stars
25.9K
Forks
3.1K
Last commit
18 hours ago
Repository age
14 years
License
MIT
Self-hosted
No
Activity score
83/100
View Repository
Ad
Favicon

 

  
 

Similar to fmt

Favicon

 

  
 
Favicon

 

  
 
Favicon