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