{fmt}

Python-style, type-safe text formatting for C++ teams tired of printf and iostreams

A modern formatting library

Category
Libraries & frameworks
Audience
Developers
Language
C++
Licence
MIT

Updated

fmt, written by its authors as {fmt}, is a C++ formatting library that gives the language one type-safe way to turn values into text, as an alternative to the C stdio printf family and to C++ iostreams. It is aimed at C++ developers who write log lines, console output, error messages and reports, and who would rather not choose between printf's crash-prone format strings and the chain-of-operators style of iostreams. The project has been going since 2012, is MIT licensed, and is one of the most widely starred C++ libraries on GitHub.

What it does

{fmt} replaces format strings of the %d and %s kind with Python-style replacement fields written in braces, and it checks them. A format string that does not match the arguments you passed is a compile error rather than a surprise at runtime, which is the failure mode the video opens with: in plain C++, a single bad printf format string can bring a program down.

Beyond the basics, the materials for this project point at several things the library covers:

  • a format API with positional arguments, so translators can reorder the pieces of a sentence without touching code
  • terminal output with colors and text styles
  • formatting for dates and durations, ranges, floating-point values and Unicode text, which are the areas the project advertises through its GitHub topics
  • speed: the video's claim is that formatting runs up to thirty times faster than printf

How it works

You write the format string once and pass the values separately, so the library — not you — decides how each type is rendered. Because the string is usually a literal known at build time, the argument count and types can be validated while the code is compiled. Type safety is a property of the design rather than a linting step bolted on afterwards.

The project treats correctness as an operational concern, not a slogan. Its repository runs continuous integration on Linux, macOS and Windows, it is continuously fuzzed through OSS-Fuzz, and it carries an OpenSSF Best Practices badge along with an OpenSSF Scorecard. For a library that sits on the path of every log line a service emits, that matters more than feature count.

Getting started

The quickest look costs nothing: the project links a ready-made Compiler Explorer snippet, so you can type a format call in a browser and watch it compile. From there the reference documentation lives at fmt.dev, and the project also points to a third-party cheat sheet on hackingcpp.com for the syntax you will use most.

Integration is deliberately undemanding. As the video puts it, the library is one header away from an existing project, so a first experiment usually means adding it to a build and converting one noisy function's output rather than planning a migration. Questions are handled publicly: the project directs users to Stack Overflow under the fmt tag rather than to a private support channel.

When to use it / when not

This is a good fit when a C++ codebase produces a lot of text — server logs, command-line tools, diagnostic dumps — and the existing output code is a mixture of printf calls and stream insertions that nobody enjoys reading. It is also a sensible default when the same source has to build on Linux, macOS and Windows, since that is the ground the project's own CI covers.

It is a weaker fit in a few situations. If your program prints a handful of lines and formatting has never been a source of bugs or profiler time, the benefit is small. If your environment forbids third-party dependencies, or your build is pinned to a toolchain you cannot move, the cost of adding a library for output is hard to justify. And a project that has already standardised on something else for formatting gains little from running two conventions side by side.

Alternatives

The two alternatives every C++ programmer already has are exactly the ones this library was written against. The stdio printf family is compact and familiar, but its format strings are unchecked text, and a mismatch between the string and the arguments is undefined behaviour rather than an error message. Iostreams are type safe, but expressing a formatted line means threading values and manipulators through a chain of insertion operators, which is verbose to write and worse to read back later. {fmt} keeps the readable single-string shape of printf and the type safety of iostreams.

Anyone maintaining a substantial C++ codebase that prints a lot should take this library seriously, and so should anyone who has spent an afternoon tracking down a crash caused by a format specifier. Twenty-five thousand stars, a permissive MIT license, continuous multi-platform testing and continuous fuzzing describe a dependency that is safe to commit to for years. The migration can start with one file, and the compiler will tell you when you get it wrong — which is the whole point.

More in Libraries & frameworks

All of Libraries & frameworks →