index.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  8. <title>Overview &mdash; fmt 9.1.0 documentation</title>
  9. <link rel="stylesheet" href="_static/basic.css" type="text/css" />
  10. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  11. <link rel="stylesheet" href="_static/breathe.css" type="text/css" />
  12. <script type="text/javascript">
  13. var DOCUMENTATION_OPTIONS = {
  14. URL_ROOT: './',
  15. VERSION: '9.1.0',
  16. COLLAPSE_INDEX: false,
  17. FILE_SUFFIX: '.html',
  18. LINK_SUFFIX: '.html',
  19. SOURCELINK_SUFFIX: '.txt',
  20. HAS_SOURCE: true,
  21. SOURCELINK_SUFFIX: '.txt'
  22. };
  23. </script>
  24. <script src="_static/jquery.js"></script>
  25. <script src="_static/underscore.js"></script>
  26. <script src="_static/doctools.js"></script>
  27. <script src="_static/language_data.js"></script>
  28. <link rel="index" title="Index" href="genindex.html" />
  29. <link rel="search" title="Search" href="search.html" />
  30. <meta name="description" content="Small, safe and fast formatting library">
  31. <meta name="keywords" content="C++, formatting, printf, string, library">
  32. <meta name="author" content="Victor Zverovich">
  33. <link rel="stylesheet" href="_static/fmt.css">
  34. <script async src="https://www.googletagmanager.com/gtag/js?id=UA-20116650-4"></script>
  35. <script>
  36. window.dataLayer = window.dataLayer || [];
  37. function gtag(){dataLayer.push(arguments);}
  38. gtag('js', new Date());
  39. gtag('config', 'UA-20116650-4');
  40. </script>
  41. </head>
  42. <body role="document">
  43. <nav class="navbar navbar-inverse">
  44. <div class="tb-container">
  45. <div class="row">
  46. <div class="navbar-content">
  47. <div class="navbar-header">
  48. <button type="button" class="navbar-toggle collapsed"
  49. data-toggle="collapse" data-target=".navbar-collapse">
  50. <span class="sr-only">Toggle navigation</span>
  51. <span class="icon-bar"></span>
  52. <span class="icon-bar"></span>
  53. <span class="icon-bar"></span>
  54. </button>
  55. <a class="navbar-brand" href="index.html">{fmt}</a>
  56. </div>
  57. <div class="collapse navbar-collapse">
  58. <ul class="nav navbar-nav">
  59. <li class="dropdown">
  60. <a href="#" class="dropdown-toggle" data-toggle="dropdown"
  61. role="button" aria-expanded="false">9.1.0
  62. <span class="caret"></span></a>
  63. <ul class="dropdown-menu" role="menu">
  64. <li><a href="https://fmt.dev/9.1.0">9.1.0</a></li>
  65. <li><a href="https://fmt.dev/9.0.0">9.0.0</a></li>
  66. <li><a href="https://fmt.dev/8.1.1">8.1.1</a></li>
  67. </ul>
  68. </li>
  69. <li><a href="contents.html">Contents</a></li>
  70. <li><a href="usage.html">Usage</a></li>
  71. <li><a href="api.html">API</a></li>
  72. <li><a href="syntax.html">Syntax</a></li>
  73. </ul>
  74. <form class="navbar-form navbar-right" role="search" action="search.html"
  75. method="get">
  76. <div class="form-group">
  77. <input type="text" name="q" class="form-control"
  78. placeholder="Search" >
  79. </div>
  80. <input type="hidden" name="check_keywords" value="yes" />
  81. <input type="hidden" name="area" value="default" />
  82. </form>
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87. </nav>
  88. <div class="jumbotron">
  89. <div class="tb-container">
  90. <h1>{fmt}</h1>
  91. <p class="lead">A modern formatting library</p>
  92. <div class="btn-group" role="group">
  93. <a class="btn btn-success"
  94. href="https://github.com/fmtlib/fmt/releases/download/9.1.0/fmt-9.1.0.zip">
  95. <span class="glyphicon glyphicon-download"></span> Download
  96. </a>
  97. <button type="button" class="btn btn-success dropdown-toggle"
  98. data-toggle="dropdown"><span class="caret"></span></button>
  99. <ul class="dropdown-menu">
  100. <li><a href="https://github.com/fmtlib/fmt/releases/download/9.1.0/fmt-9.1.0.zip">Version 9.1.0
  101. </a></li>
  102. <li><a href="https://github.com/fmtlib/fmt/releases/download/9.0.0/fmt-9.0.0.zip">Version 9.0.0
  103. </a></li>
  104. <li><a href="https://github.com/fmtlib/fmt/releases/download/8.1.1/fmt-8.1.1.zip">Version 8.1.1
  105. </a></li>
  106. </ul>
  107. </div>
  108. </div>
  109. </div>
  110. <div class="tb-container">
  111. <div class="row">
  112. <div class="content">
  113. <section id="overview">
  114. <h1>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h1>
  115. <p><strong>{fmt}</strong> is an open-source formatting library providing a fast and safe
  116. alternative to C stdio and C++ iostreams.</p>
  117. <div class="panel panel-default">
  118. <div class="panel-heading">What users say:</div>
  119. <div class="panel-body">
  120. Thanks for creating this library. It’s been a hole in C++ for
  121. a long time. I’ve used both <code>boost::format</code> and
  122. <code>loki::SPrintf</code>, and neither felt like the right answer.
  123. This does.
  124. </div>
  125. </div><section id="format-api">
  126. <span id="format-api-intro"></span><h2>Format API<a class="headerlink" href="#format-api" title="Permalink to this headline">¶</a></h2>
  127. <p>The format API is similar in spirit to the C <code class="docutils literal notranslate"><span class="pre">printf</span></code> family of function but
  128. is safer, simpler and several times <a class="reference external" href="https://www.zverovich.net/2020/06/13/fast-int-to-string-revisited.html">faster</a>
  129. than common standard library implementations.
  130. The <a class="reference external" href="syntax.html">format string syntax</a> is similar to the one used by
  131. <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str.format">str.format</a> in
  132. Python:</p>
  133. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;The answer is {}.&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">);</span><span class="w"></span>
  134. </pre></div>
  135. </div>
  136. <p>The <code class="docutils literal notranslate"><span class="pre">fmt::format</span></code> function returns a string “The answer is 42.”. You can use
  137. <code class="docutils literal notranslate"><span class="pre">fmt::memory_buffer</span></code> to avoid constructing <code class="docutils literal notranslate"><span class="pre">std::string</span></code>:</p>
  138. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="k">auto</span><span class="w"> </span><span class="n">out</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fmt</span><span class="o">::</span><span class="n">memory_buffer</span><span class="p">();</span><span class="w"></span>
  139. <span class="n">fmt</span><span class="o">::</span><span class="n">format_to</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">back_inserter</span><span class="p">(</span><span class="n">out</span><span class="p">),</span><span class="w"></span>
  140. <span class="w"> </span><span class="s">&quot;For a moment, {} happened.&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;nothing&quot;</span><span class="p">);</span><span class="w"></span>
  141. <span class="k">auto</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">out</span><span class="p">.</span><span class="n">data</span><span class="p">();</span><span class="w"> </span><span class="c1">// pointer to the formatted data</span>
  142. <span class="k">auto</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">out</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="w"> </span><span class="c1">// size of the formatted data</span>
  143. </pre></div>
  144. </div>
  145. <p>The <code class="docutils literal notranslate"><span class="pre">fmt::print</span></code> function performs formatting and writes the result to a stream:</p>
  146. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;System error code = {}</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">errno</span><span class="p">);</span><span class="w"></span>
  147. </pre></div>
  148. </div>
  149. <p>If you omit the file argument the function will print to <code class="docutils literal notranslate"><span class="pre">stdout</span></code>:</p>
  150. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="s">&quot;Don&#39;t {}</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;panic&quot;</span><span class="p">);</span><span class="w"></span>
  151. </pre></div>
  152. </div>
  153. <p>The format API also supports positional arguments useful for localization:</p>
  154. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="s">&quot;I&#39;d rather be {1} than {0}.&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;right&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;happy&quot;</span><span class="p">);</span><span class="w"></span>
  155. </pre></div>
  156. </div>
  157. <p>You can pass named arguments with <code class="docutils literal notranslate"><span class="pre">fmt::arg</span></code>:</p>
  158. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="s">&quot;Hello, {name}! The answer is {number}. Goodbye, {name}.&quot;</span><span class="p">,</span><span class="w"></span>
  159. <span class="w"> </span><span class="n">fmt</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;World&quot;</span><span class="p">),</span><span class="w"> </span><span class="n">fmt</span><span class="o">::</span><span class="n">arg</span><span class="p">(</span><span class="s">&quot;number&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">));</span><span class="w"></span>
  160. </pre></div>
  161. </div>
  162. <p>If your compiler supports C++11 user-defined literals, the suffix <code class="docutils literal notranslate"><span class="pre">_a</span></code> offers
  163. an alternative, slightly terser syntax for named arguments:</p>
  164. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="k">using</span><span class="w"> </span><span class="k">namespace</span><span class="w"> </span><span class="nn">fmt</span><span class="o">::</span><span class="nn">literals</span><span class="p">;</span><span class="w"></span>
  165. <span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="s">&quot;Hello, {name}! The answer is {number}. Goodbye, {name}.&quot;</span><span class="p">,</span><span class="w"></span>
  166. <span class="w"> </span><span class="s">&quot;name&quot;</span><span class="n">_a</span><span class="o">=</span><span class="s">&quot;World&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;number&quot;</span><span class="n">_a</span><span class="o">=</span><span class="mi">42</span><span class="p">);</span><span class="w"></span>
  167. </pre></div>
  168. </div>
  169. </section>
  170. <section id="safety">
  171. <span id="id1"></span><h2>Safety<a class="headerlink" href="#safety" title="Permalink to this headline">¶</a></h2>
  172. <p>The library is fully type safe, automatic memory management prevents buffer
  173. overflow, errors in format strings are reported using exceptions or at compile
  174. time. For example, the code</p>
  175. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;The answer is {:d}&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;forty-two&quot;</span><span class="p">);</span><span class="w"></span>
  176. </pre></div>
  177. </div>
  178. <p>throws the <code class="docutils literal notranslate"><span class="pre">format_error</span></code> exception because the argument <code class="docutils literal notranslate"><span class="pre">&quot;forty-two&quot;</span></code> is a
  179. string while the format code <code class="docutils literal notranslate"><span class="pre">d</span></code> only applies to integers.</p>
  180. <p>The code</p>
  181. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">format</span><span class="p">(</span><span class="n">FMT_STRING</span><span class="p">(</span><span class="s">&quot;The answer is {:d}&quot;</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;forty-two&quot;</span><span class="p">);</span><span class="w"></span>
  182. </pre></div>
  183. </div>
  184. <p>reports a compile-time error on compilers that support relaxed <code class="docutils literal notranslate"><span class="pre">constexpr</span></code>.
  185. See <a class="reference external" href="api.html#compile-time-format-string-checks">here</a> for details.</p>
  186. <p>The following code</p>
  187. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span>fmt::format(&quot;Cyrillic letter {}&quot;, L&#39;\x42e&#39;);
  188. </pre></div>
  189. </div>
  190. <p>produces a compile-time error because wide character <code class="docutils literal notranslate"><span class="pre">L'\x42e'</span></code> cannot be
  191. formatted into a narrow string. For comparison, writing a wide character to
  192. <code class="docutils literal notranslate"><span class="pre">std::ostream</span></code> results in its numeric value being written to the stream
  193. (i.e. 1070 instead of letter ‘ю’ which is represented by <code class="docutils literal notranslate"><span class="pre">L'\x42e'</span></code> if we
  194. use Unicode) which is rarely desirable.</p>
  195. </section>
  196. <section id="compact-binary-code">
  197. <h2>Compact Binary Code<a class="headerlink" href="#compact-binary-code" title="Permalink to this headline">¶</a></h2>
  198. <p>The library produces compact per-call compiled code. For example
  199. (<a class="reference external" href="https://godbolt.org/g/TZU4KF">godbolt</a>),</p>
  200. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;fmt/core.h&gt;</span><span class="cp"></span>
  201. <span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
  202. <span class="w"> </span><span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="s">&quot;The answer is {}.&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">);</span><span class="w"></span>
  203. <span class="p">}</span><span class="w"></span>
  204. </pre></div>
  205. </div>
  206. <p>compiles to just</p>
  207. <div class="highlight-asm notranslate"><div class="highlight"><pre><span></span>main: # @main
  208. sub rsp, 24
  209. mov qword ptr [rsp], 42
  210. mov rcx, rsp
  211. mov edi, offset .L.str
  212. mov esi, 17
  213. mov edx, 1
  214. call fmt::v7::vprint(fmt::v7::basic_string_view&lt;char&gt;, fmt::v7::format_args)
  215. xor eax, eax
  216. add rsp, 24
  217. ret
  218. .L.str:
  219. .asciz &quot;The answer is {}.&quot;
  220. </pre></div>
  221. </div>
  222. </section>
  223. <section id="portability">
  224. <span id="id2"></span><h2>Portability<a class="headerlink" href="#portability" title="Permalink to this headline">¶</a></h2>
  225. <p>The library is highly portable and relies only on a small set of C++11 features:</p>
  226. <ul class="simple">
  227. <li><p>variadic templates</p></li>
  228. <li><p>type traits</p></li>
  229. <li><p>rvalue references</p></li>
  230. <li><p>decltype</p></li>
  231. <li><p>trailing return types</p></li>
  232. <li><p>deleted functions</p></li>
  233. <li><p>alias templates</p></li>
  234. </ul>
  235. <p>These are available in GCC 4.8, Clang 3.4, MSVC 19.0 (2015) and more recent
  236. compiler version. For older compilers use {fmt} <a class="reference external" href="https://github.com/fmtlib/fmt/releases/tag/4.1.0">version 4.x</a> which is maintained and
  237. only requires C++98.</p>
  238. <p>The output of all formatting functions is consistent across platforms.
  239. For example,</p>
  240. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">numeric_limits</span><span class="o">&lt;</span><span class="kt">double</span><span class="o">&gt;::</span><span class="n">infinity</span><span class="p">());</span><span class="w"></span>
  241. </pre></div>
  242. </div>
  243. <p>always prints <code class="docutils literal notranslate"><span class="pre">inf</span></code> while the output of <code class="docutils literal notranslate"><span class="pre">printf</span></code> is platform-dependent.</p>
  244. </section>
  245. <section id="ease-of-use">
  246. <span id="id3"></span><h2>Ease of Use<a class="headerlink" href="#ease-of-use" title="Permalink to this headline">¶</a></h2>
  247. <p>{fmt} has a small self-contained code base with the core library consisting of
  248. just three header files and no external dependencies.
  249. A permissive MIT <a class="reference external" href="https://github.com/fmtlib/fmt#license">license</a> allows
  250. using the library both in open-source and commercial projects.</p>
  251. <p><a class="reference external" href="contents.html">Learn more…</a></p>
  252. <a class="btn btn-success" href="https://github.com/fmtlib/fmt">GitHub Repository</a>
  253. <div class="section footer">
  254. <iframe src="https://ghbtns.com/github-btn.html?user=fmtlib&amp;repo=fmt&amp;type=watch&amp;count=true"
  255. class="github-btn" width="100" height="20"></iframe>
  256. </div></section>
  257. </section>
  258. </div>
  259. </div>
  260. </div>
  261. <div class="footer" role="contentinfo">
  262. &copy; Copyright 2012-present, Victor Zverovich.
  263. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 3.3.0.
  264. </div>
  265. <script src="_static/bootstrap.min.js"></script>
  266. </body>
  267. </html>