7.0.0
-
Reduced the library size. For example, on macOS a stripped test binary statically linked with {fmt} shrank from ~368k to less than 100k.
-
Added a simpler and more efficient format string compilation API:
#include <fmt/compile.h> // Converts 42 into std::string using the most efficient method and no // runtime format string processing. std::string s = fmt::format(FMT_COMPILE("{}"), 42);
The old
fmt::compile
API is now deprecated.Thanks @ldionne (Louis Dionne) for the suggestion and @hanickadot (Hana Dusíková) for the initial prototype.
-
Optimized integer formatting:
format_to
with format string compilation and a stack-allocated buffer is now faster than to_chars on both libc++ and libstdc++. -
Optimized handling of small format strings. For example,
fmt::format("Result: {}: ({},{},{},{})", str1, str2, str3, str4, str5)
is now ~40% faster (#1685).
-
Applied extern templates to improve compile times when using the core API and
fmt/format.h
(#1452). For example, on macOS with clang the compile time of a test translation unit dropped from 2.3s to 0.3s with-O2
and from 0.6s to 0.3s with the default settings (-O0
).Before (
-O2
):% time c++ -c test.cc -I include -std=c++17 -O2 c++ -c test.cc -I include -std=c++17 -O2 2.22s user 0.08s system 99% cpu 2.311 total
After (
-O2
):% time c++ -c test.cc -I include -std=c++17 -O2 c++ -c test.cc -I include -std=c++17 -O2 0.26s user 0.04s system 98% cpu 0.303 total
Before (default):
% time c++ -c test.cc -I include -std=c++17 c++ -c test.cc -I include -std=c++17 0.53s user 0.06s system 98% cpu 0.601 total
After (default):
% time c++ -c test.cc -I include -std=c++17 c++ -c test.cc -I include -std=c++17 0.24s user 0.06s system 98% cpu 0.301 total
It is still recommended to use
fmt/core.h
instead offmt/format.h
but the compile time difference is now smaller. Thanks @alex3d for the suggestion. -
Named arguments are now stored on stack (no dynamic memory allocations) and the compiled code is more compact and efficient. For example
#include <fmt/core.h> int main() { fmt::print("The answer is {answer}\n", fmt::arg("answer", 42)); }
compiles to just (godbolt)
.LC0: .string "answer" .LC1: .string "The answer is {answer}\n" main: sub rsp, 56 mov edi, OFFSET FLAT:.LC1 mov esi, 23 movabs rdx, 4611686018427387905 lea rax, [rsp+32] lea rcx, [rsp+16] mov QWORD PTR [rsp+8], 1 mov QWORD PTR [rsp], rax mov DWORD PTR [rsp+16], 42 mov QWORD PTR [rsp+32], OFFSET FLAT:.LC0 mov DWORD PTR [rsp+40], 0 call fmt::v6::vprint(fmt::v6::basic_string_view<char>, fmt::v6::format_args) xor eax, eax add rsp, 56 ret .L.str.1: .asciz "answer"
-
Implemented compile-time checks for dynamic width and precision (#1614):
#include <fmt/format.h> int main() { fmt::print(FMT_STRING("{0:{1}}"), 42); }
now gives a compilation error because argument 1 doesn't exist:
In file included from test.cc:1: include/fmt/format.h:2726:27: error: constexpr variable 'invalid_format' must be initialized by a constant expression FMT_CONSTEXPR_DECL bool invalid_format = ^ ... include/fmt/core.h:569:26: note: in call to '&checker(s, {}).context_->on_error(&"argument not found"[0])' if (id >= num_args_) on_error("argument not found"); ^
-
Added sentinel support to
fmt::join
(#1689)struct zstring_sentinel {}; bool operator==(const char* p, zstring_sentinel) { return *p == '\0'; } bool operator!=(const char* p, zstring_sentinel) { return *p != '\0'; } struct zstring { const char* p; const char* begin() const { return p; } zstring_sentinel end() const { return {}; } }; auto s = fmt::format("{}", fmt::join(zstring{"hello"}, "_")); // s == "h_e_l_l_o"
Thanks @BRevzin (Barry Revzin).
-
Added support for named args,
clear
andreserve
todynamic_format_arg_store
(#1655, #1663, #1674, #1677). Thanks @vsolontsov-ll (Vladimir Solontsov). -
Added support for the
'c'
format specifier to integral types for compatibility withstd::format
(#1652). -
Replaced the
'n'
format specifier with'L'
for compatibility withstd::format
(#1624). The'n'
specifier can be enabled via theFMT_DEPRECATED_N_SPECIFIER
macro. -
The
'='
format specifier is now disabled by default for compatibility withstd::format
. It can be enabled via theFMT_DEPRECATED_NUMERIC_ALIGN
macro. -
Removed the following deprecated APIs:
-
FMT_STRING_ALIAS
andfmt
macros - replaced byFMT_STRING
-
fmt::basic_string_view::char_type
- replaced byfmt::basic_string_view::value_type
-
convert_to_int
-
format_arg_store::types
-
*parse_context
- replaced by*format_parse_context
-
FMT_DEPRECATED_INCLUDE_OS
-
FMT_DEPRECATED_PERCENT
- incompatible withstd::format
-
*writer
- replaced by compiled format API
-
-
Renamed the
internal
namespace todetail
(#1538). The former is still provided as an alias if theFMT_USE_INTERNAL
macro is defined. -
Improved compatibility between
fmt::printf
with the standard specs (#1595, #1682, #1683, #1687, #1699). Thanks @rimathia. -
Fixed handling of
operator<<
overloads that usecopyfmt
(#1666). -
Added the
FMT_OS
CMake option to control inclusion of OS-specific APIs in the fmt target. This can be useful for embedded platforms (#1654, #1656). Thanks @kwesolowski (Krzysztof Wesolowski). -
Replaced
FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
with theFMT_FUZZ
macro to prevent interferring with fuzzing of projects using {fmt} (#1650). Thanks @asraa (Asra Ali). -
Fixed compatibility with emscripten (#1636, #1637). Thanks @ArthurSonzogni (Arthur Sonzogni).
-
Improved documentation (#704, #1643, #1660, #1681, #1691, #1706, #1714, #1721, #1739, #1740, #1741, #1751). Thanks @senior7515 (Alexander Gallego), @lsr0 (Lindsay Roberts), @puetzk (Kevin Puetz), @fpelliccioni (Fernando Pelliccioni), Alexey Kuzmenko, @jelly (jelle van der Waa), @claremacrae (Clare Macrae), @jiapengwen (文佳鹏), @gsjaardema (Greg Sjaardema), @alexey-milovidov.
-
Implemented various build configuration fixes and improvements (#1603, #1657, #1702, #1728). Thanks @scramsby (Scott Ramsby), @jtojnar (Jan Tojnar), @orivej (Orivej Desh), @flagarde.
-
Fixed various warnings and compilation issues (#1616, #1620, #1622, #1625, #1627, #1628, #1629, #1631, #1633, #1649, #1658, #1661, #1667, #1668, #1669, #1692, #1696, #1697, #1707, #1712, #1716, #1722, #1724, #1729, #1738, #1742, #1743, #1744, #1747, #1750). Thanks @gsjaardema (Greg Sjaardema), @gabime (Gabi Melman), @johnor (Johan), @gabime (Dmitry Kurkin), @invexed (James Beach), @peterbell10, @daixtrose (Markus Werle), @petrutlucian94 (Lucian Petrut), @Neargye (Daniil Goncharov), @ambitslix (Attila M. Szilagyi), @gabime (Gabi Melman), @erthink (Leonid Yuriev), @tohammer (Tobias Hammer), @0x8000-0000 (Florin Iucha).