14 | Inspired by Python's formatting facility, {fmt} provides a safe replacement
15 | for the printf family of functions. Errors in format strings,
16 | which are a common source of vulnerabilities in C, are reported at
17 | compile time. For example:
18 |
19 |
fmt::format("{:d}", "I am not a number");
21 |
22 | will give a compile-time error because d is not a valid
23 | format specifier for strings. APIs like
24 | fmt::format prevent buffer overflow errors via
25 | automatic memory management.
26 |
27 | → Learn more
28 | 33 | Formatting of most standard types, including all containers, dates, 34 | and times is supported out-of-the-box. For example: 35 | 36 |
fmt::print("{}", std::vector{1, 2, 3});
38 |
39 | prints the vector in a JSON-like format:
40 |
41 | [1, 2, 3]
42 |
43 | You can make your own types formattable and even make compile-time
44 | checks work for them.
45 |
46 | → Learn more
47 |
52 | {fmt} can be anywhere from tens of percent to 20-30 times faster than
53 | iostreams and sprintf, especially for numeric formatting.
54 |
55 |
56 |
57 |
58 |
59 | The library minimizes dynamic memory allocations and can optionally
60 | compile format strings to optimal code.
61 |
67 | {fmt} provides portable Unicode support on major operating systems
68 | with UTF-8 and char strings. For example:
69 |
70 |
fmt::print("Слава Україні!");
72 |
73 | will be printed correctly on Linux, macOS, and even Windows console,
74 | irrespective of the codepages.
75 |
76 | 77 | The default is locale-independent, but you can opt into localized 78 | formatting and {fmt} makes it work with Unicode, addressing issues in the 79 | standard libary. 80 |
81 |
86 | The library makes extensive use of type erasure to achieve fast
87 | compilation. fmt/base.h provides a subset of the API with
88 | minimal include dependencies and enough functionality to replace
89 | all uses of *printf.
90 |
92 | Code using {fmt} is usually several times faster to compile than the
93 | equivalent iostreams code, and while printf compiles faster
94 | still, the gap is narrowing.
95 |
104 | Type erasure is also used to prevent template bloat, resulting in compact
105 | per-call binary code. For example, a call to fmt::print with
106 | a single argument is just a few
107 | instructions, comparable to printf despite adding
108 | runtime safety, and much smaller than the equivalent iostreams code.
109 |
111 | The library itself has small binary footprint and some components such as 112 | floating-point formatting can be disabled to make it even smaller for 113 | resource-constrained devices. 114 |
115 |120 | {fmt} has a small self-contained codebase with the core consisting of 121 | just three headers and no external dependencies. 122 |
123 |124 | The library is highly portable and requires only a minimal subset of 125 | C++11 features which are available in GCC 4.9, Clang 3.6, MSVC 19.10 126 | (2017) and later. Newer compiler and standard library features are used 127 | if available, and enable additional functionality. 128 |
129 |130 | Where possible, the output of formatting functions is consistent across 131 | platforms. 132 |
133 | 134 |139 | {fmt} is in the top hundred open-source C++ libraries on GitHub and has 140 | hundreds of 141 | all-time contributors. 142 |
143 |144 | The library is distributed under a permissive MIT 145 | license and is 146 | relied upon by many open-source projects, including Blender, PyTorch, 147 | Apple's FoundationDB, Windows Terminal, MongoDB, and others. 148 |
149 |