Format C++ text quickly and safely
Converting variable values to strings can become a bottleneck when logging or saving data to files. This library handles that task faster than the C++ standard library's output features. It implements the C++20 std::format and C++23 std::print standards in advance, allowing you to use consistent formatting without waiting for the latest standards.
Early implementation of standard formatting
This library provides the std::format and std::print features included in the C++20 and C++23 standards before the compilers do. This allows you to compose strings using syntax similar to Python's format method, even if your compiler does not yet support the standard. You can specify values by index using positional arguments when localization is required. This feature provides a more intuitive syntax than C's printf or C++'s iostream.
Accuracy and speed of number conversion
Accuracy and speed are most important when converting floating-point numbers to strings. This library uses the Dragonbox algorithm to convert IEEE 754 floating-point values. This method guarantees correct rounding, the shortest possible result string, and that converting the result back to a number matches the original value. It is tens of percent to 20 to 30 times faster than sprintf or iostream, with the difference being particularly large in number formatting. It minimizes dynamic memory allocation and provides an option to compile format strings into efficient code.
Compile-time safety
Errors in format strings are detected at the compilation stage. For example, if you use a number-only format spec on a string variable, the compiler reports an error. This prevents buffer overflows from occurring at runtime. The library has complete type safety and reduces the risk of memory leaks or overflows through automatic memory management. It maintains stability through extensive testing and continuous fuzzing.
Scope of use and extensibility
In addition to basic string formatting, it supports dates and times, containers, and colored text output. Including fmt/chrono.h allows you to print std::chrono objects directly, and using fmt/ranges.h prints containers like vectors in the form [1, 2, 3]. Using fmt/color.h lets you apply text styles such as color, bold, and underline in the terminal. Custom types can be easily integrated into formatting, offering good extensibility. The minimal configuration consists of three files: core.h, format.h, and format-inl.h, keeping the code size small.
Considerations before adoption
This library uses the MIT license and has no external dependencies. If you want a header-only configuration, define the FMT_HEADER_ONLY macro. When comparing compile time and binary size, building as a module can reduce compile time by 27% in optimized builds. Measurements in an Apple M5 Max environment show that compile time is similar to or shorter than printf, and binary size is similar to or smaller. When introducing it to an existing project, you must check C++ standard version and compiler compatibility, especially since the compile-time check feature only works fully with C++20 or later.