C++ 格式化库:cppformat

jopen 6年前

cppformat (C++ Format) 是一个既轻便、安全,又快捷的开源 C++ 格式化库。它可以用来取代 IOStreams ,同时也是格式输出中较为安全的替代品。


  • Two APIs: faster concatenation-based write API and slower (but still very fast) replacement-based format API with positional arguments for localization.
  • Write API similar to the one used by IOStreams but stateless allowing faster implementation.
  • Format API with format string syntax similar to the one used by str.format in Python.
  • Safe printf implementation including the POSIX extension for positional arguments.
  • Support for user-defined types.
  • High speed: performance of the format API is close to that of glibc's printf and better than performance of IOStreams. See Speed tests and Fast integer to string conversion in C++.
  • Small code size both in terms of source code (format consists of a single header file and a single source file) and compiled code. See Compile time and code bloat.
  • Reliability: the library has an extensive set of unit tests.
  • Safety: the library is fully type safe, errors in format strings are reported using exceptions, automatic memory management prevents buffer overflow errors.
  • Ease of use: small self-contained code base, no external dependencies, permissive BSD license.
  • Portability with consistent output across platforms and support for older compilers.
  • Clean warning-free codebase even on high warning levels (-Wall -Wextra -pedantic).
  • Support for wide strings.
  • Optional header-only configuration enabled with theFMT_HEADER_ONLYmacro.


This prints Hello, world! to stdout:

fmt::print("Hello, {}!", "world");  // uses Python-like format string syntax  fmt::printf("Hello, %s!", "world"); // uses printf format string syntax