C++ Format

Small, safe and fast formatting library for C++

C++ Format (cppformat) is an open-source formatting library for C++. It can be used as a safe alternative to printf or as a fast alternative to IOStreams.

Format API

The replacement-based Format API provides a safe alternative to printf, sprintf and friends with comparable or better performance. The format string syntax is similar to the one used by str.format in Python:

format("The answer is {}", 42);

The format function returns a string "The answer is 42". You can use MemoryWriter to avoid constructing std::string:

fmt::MemoryWriter w;
w.write("Look, a {} string", 'C');
w.c_str(); // returns a C string (const char*)

The print function performs formatting and writes the result to a file:

print(stderr, "System error code = {}\n", errno);

The file argument can be omitted in which case the function prints to stdout:

print("Don't {}\n", "panic");

If your compiler supports C++11, then the formatting functions are implemented with variadic templates. Otherwise variadic functions are emulated by generating a set of lightweight wrappers. This ensures compatibility with older compilers while providing a natural API.

The Format API also supports positional arguments useful for localization:

print("I'd rather be {1} than {0}.", "right", "happy");

Write API

The concatenation-based Write API (experimental) provides a fast stateless alternative to IOStreams:

MemoryWriter out;
out << "The answer in hexadecimal is " << hex(42);

Safety

The library is fully type safe, automatic memory management prevents buffer overflow, errors in format strings are reported using exceptions. For example, the code

format("The answer is {:d}", "forty-two");

throws a FormatError exception with description "unknown format code 'd' for string", because the argument "forty-two" is a string while the format code d only applies to integers.

Where possible, errors are catched at compile time. For example, the code

format("Cyrillic letter {}", L'\x42e');

produces a compile-time error because wide character L'\x42e' cannot be formatted into a narrow string. You can use a wide format string instead:

format(L"Cyrillic letter {}", L'\x42e');

For comparison, writing a wide character to std::ostream results in its numeric value being written to the stream (i.e. 1070 instead of letter 'ю' which is represented by L'\x42e' if we use Unicode) which is rarely what is needed.

Portability

C++ Format is highly portable. Here is an incomplete list of operating systems and compilers where it has been tested and known to work:

  • 64-bit (amd64) GNU/Linux with GCC 4.4.3, 4.6.3, 4.7.2, 4.8.1 and Intel C++ Compiler (ICC) 14.0.2
  • 32-bit (i386) GNU/Linux with GCC 4.4.3, 4.6.3
  • Mac OS X with GCC 4.2.1 and Clang 4.2, 5.1.0
  • 64-bit Windows with Visual C++ 2010 and 2013
  • 32-bit Windows with Visual C++ 2010

Although the library uses C++11 features when available, it also works with older compilers and standard library implementations.

The output of all formatting functions is consistent across platforms. In particular, formatting a floating-point infinity always gives inf while the output of printf is platform-dependent in this case. For example,

print("{}", std::numeric_limits<double>::infinity());

always prints inf.

Ease of Use

C++ Format has small self-contained code base consisting of a single header file and a single source file and no external dependencies. A permissive BSD license allows using the library both in open-source and commercial projects.

Learn more