C++ Format v6.0.0 Release Notes

Release Date: 2019-08-26 // over 4 years ago
    • Switched to the MIT license <https://github.com/fmtlib/fmt/blob/5a4b24613ba16cc689977c3b5bd8274a3ba1dd1f/LICENSE.rst>_ with an optional exception that allows distributing binary code without attribution.

    • 0๏ธโƒฃ Floating-point formatting is now locale-independent by default:

    .. code:: c++

     #include <locale>
     #include <fmt/core.h>
    
     int main() {
       std::locale::global(std::locale("ru_RU.UTF-8"));
       fmt::print("value = {}", 4.2);
     }
    

    prints "value = 4.2" regardless of the locale.

    For locale-specific formatting use the n specifier:

    .. code:: c++

     std::locale::global(std::locale("ru_RU.UTF-8"));
     fmt::print("value = {:n}", 4.2);
    

    prints "value = 4,2".

    • โž• Added an experimental Grisu floating-point formatting algorithm implementation (disabled by default). To enable it compile with the FMT_USE_GRISU macro defined to 1:

    .. code:: c++

     #define FMT_USE_GRISU 1
     #include <fmt/format.h>
    
     auto s = fmt::format("{}", 4.2); // formats 4.2 using Grisu
    

    With Grisu enabled, {fmt} is 13x faster than std::ostringstream (libc++) and 10x faster than sprintf on dtoa-benchmark <https://github.com/fmtlib/dtoa-benchmark>_ (full results <https://fmt.dev/unknown_mac64_clang10.0.html>_):

    .. image:: https://user-images.githubusercontent.com/576385/ 54883977-9fe8c000-4e28-11e9-8bde-272d122e7c52.jpg

    • ๐Ÿ“œ Separated formatting and parsing contexts for consistency with C++20 std::format <http://eel.is/c++draft/format>_, removing the undocumented basic_format_context::parse_context() function.

    • โž• Added oss-fuzz <https://github.com/google/oss-fuzz>_ support (#1199 <https://github.com/fmtlib/fmt/pull/1199>). Thanks @pauldreik (Paul Dreik) <https://github.com/pauldreik>.

    • formatter specializations now always take precedence over operator<< (#952 <https://github.com/fmtlib/fmt/issues/952>_):

    .. code:: c++

     #include <iostream>
     #include <fmt/ostream.h>
    
     struct S {};
    
     std::ostream& operator<<(std::ostream& os, S) {
       return os << 1;
     }
    
     template <>
     struct fmt::formatter<S> : fmt::formatter<int> {
       auto format(S, format_context& ctx) {
         return formatter<int>::format(2, ctx);
       }
     };
    
     int main() {
       std::cout << S() << "\n"; // prints 1 using operator<<
       fmt::print("{}\n", S());  // prints 2 using formatter
     }
    
    • Introduced the experimental fmt::compile function that does format string compilation (#618 <https://github.com/fmtlib/fmt/issues/618>, #1169 <https://github.com/fmtlib/fmt/issues/1169>, #1171 <https://github.com/fmtlib/fmt/pull/1171>_):

    .. code:: c++

     #include <fmt/compile.h>
    
     auto f = fmt::compile<int>("{}");
     std::string s = fmt::format(f, 42); // can be called multiple times to
                                         // format different values
     // s == "42"
    

    It moves the cost of parsing a format string outside of the format function which can be beneficial when identically formatting many objects of the same types. Thanks @stryku (Mateusz Janek) <https://github.com/stryku>_.

    • โž• Added experimental % format specifier that formats floating-point values as percentages (#1060 <https://github.com/fmtlib/fmt/pull/1060>, #1069 <https://github.com/fmtlib/fmt/pull/1069>, #1071 <https://github.com/fmtlib/fmt/pull/1071>_):

    .. code:: c++

     auto s = fmt::format("{:.1%}", 0.42); // s == "42.0%"
    

    Thanks @gawain-bolton (Gawain Bolton) <https://github.com/gawain-bolton>_.

    • Implemented precision for floating-point durations (#1004 <https://github.com/fmtlib/fmt/issues/1004>, #1012 <https://github.com/fmtlib/fmt/pull/1012>):

    .. code:: c++

     auto s = fmt::format("{:.1}", std::chrono::duration<double>(1.234));
     // s == 1.2s
    

    Thanks @DanielaE (Daniela Engert) <https://github.com/DanielaE>_.

    • Implemented chrono format specifiers %Q and %q that give the value and the unit respectively (#1019 <https://github.com/fmtlib/fmt/pull/1019>_):

    .. code:: c++

     auto value = fmt::format("{:%Q}", 42s); // value == "42"
     auto unit  = fmt::format("{:%q}", 42s); // unit == "s"
    

    Thanks @DanielaE (Daniela Engert) <https://github.com/DanielaE>_.

    • ๐Ÿ›  Fixed handling of dynamic width in chrono formatter:

    .. code:: c++

     auto s = fmt::format("{0:{1}%H:%M:%S}", std::chrono::seconds(12345), 12);
     //                        ^ width argument index                     ^ width
     // s == "03:25:45    "
    

    Thanks Howard Hinnant.

    • โœ‚ Removed deprecated fmt/time.h. Use fmt/chrono.h instead.

    • โž• Added fmt::format and fmt::vformat overloads that take text_style (#993 <https://github.com/fmtlib/fmt/issues/993>, #994 <https://github.com/fmtlib/fmt/pull/994>):

    .. code:: c++

     #include <fmt/color.h>
    
     std::string message = fmt::format(fmt::emphasis::bold | fg(fmt::color::red),
                                       "The answer is {}.", 42);
    

    Thanks @Naios (Denis Blank) <https://github.com/Naios>_.

    • โœ‚ Removed the deprecated color API (print_colored). Use the new API, namely print overloads that take text_style instead.

    • Made std::unique_ptr and std::shared_ptr formattable as pointers via fmt::ptr (#1121 <https://github.com/fmtlib/fmt/pull/1121>_):

    .. code:: c++

     std::unique_ptr<int> p = ...;
     fmt::print("{}", fmt::ptr(p)); // prints p as a pointer
    

    Thanks @sighingnow (Tao He) <https://github.com/sighingnow>_.

    • ๐Ÿ–จ Made print and vprint report I/O errors (#1098 <https://github.com/fmtlib/fmt/issues/1098>, #1099 <https://github.com/fmtlib/fmt/pull/1099>). Thanks @BillyDonahue (Billy Donahue) <https://github.com/BillyDonahue>_.

    • ๐Ÿšš Marked deprecated APIs with the [[deprecated]] attribute and removed internal uses of deprecated APIs (#1022 <https://github.com/fmtlib/fmt/pull/1022>). Thanks @eliaskosunen (Elias Kosunen) <https://github.com/eliaskosunen>.

    • โ†ช Modernized the codebase using more C++11 features and removing workarounds. Most importantly, buffer_context is now an alias template, so use buffer_context<T> instead of buffer_context<T>::type. These features require GCC 4.8 or later.

    • formatter specializations now always take precedence over implicit conversions to int and the undocumented convert_to_int trait is now deprecated.

    • ๐Ÿšš Moved the undocumented basic_writer, writer, and wwriter types to the internal namespace.

    • Removed deprecated basic_format_context::begin(). Use out() instead.

    • Disallowed passing the result of join as an lvalue to prevent misuse.

    • ๐Ÿ”จ Refactored the undocumented structs that represent parsed format specifiers to simplify the API and allow multibyte fill.

    • ๐Ÿšš Moved SFINAE to template parameters to reduce symbol sizes.

    • Switched to fputws for writing wide strings so that it's no longer required to call _setmode on Windows (#1229 <https://github.com/fmtlib/fmt/issues/1229>, #1243 <https://github.com/fmtlib/fmt/pull/1243>). Thanks @jackoalan (Jack Andersen) <https://github.com/jackoalan>_.

    • ๐Ÿ‘Œ Improved literal-based API (#1254 <https://github.com/fmtlib/fmt/pull/1254>). Thanks @sylveon (Charles Milette) <https://github.com/sylveon>.

    • โž• Added support for exotic platforms without uintptr_t such as IBM i (AS/400) which has 128-bit pointers and only 64-bit integers (#1059 <https://github.com/fmtlib/fmt/issues/1059>_).

    • โž• Added Sublime Text syntax highlighting config <https://github.com/fmtlib/fmt/blob/master/support/C%2B%2B.sublime-syntax>_ (#1037 <https://github.com/fmtlib/fmt/issues/1037>). Thanks @Kronuz (Germรกn Mรฉndez Bravo) <https://github.com/Kronuz>.

    • Added the FMT_ENFORCE_COMPILE_STRING macro to enforce the use of compile-time format strings (#1231 <https://github.com/fmtlib/fmt/pull/1231>). Thanks @jackoalan (Jack Andersen) <https://github.com/jackoalan>.

    • ๐Ÿ— Stopped setting CMAKE_BUILD_TYPE if {fmt} is a subproject (#1081 <https://github.com/fmtlib/fmt/issues/1081>_).

    • ๐Ÿ— Various build improvements (#1039 <https://github.com/fmtlib/fmt/pull/1039>, #1078 <https://github.com/fmtlib/fmt/pull/1078>, #1091 <https://github.com/fmtlib/fmt/pull/1091>, #1103 <https://github.com/fmtlib/fmt/pull/1103>, #1177 <https://github.com/fmtlib/fmt/pull/1177>). Thanks @luncliff (Park DongHa) <https://github.com/luncliff>, @jasonszang (Jason Shuo Zang) <https://github.com/jasonszang>, @olafhering (Olaf Hering) <https://github.com/olafhering>, @Lecetem <https://github.com/Lectem>, @pauldreik (Paul Dreik) <https://github.com/pauldreik>.

    • ๐Ÿ‘Œ Improved documentation (#1049 <https://github.com/fmtlib/fmt/issues/1049>, #1051 <https://github.com/fmtlib/fmt/pull/1051>, #1083 <https://github.com/fmtlib/fmt/pull/1083>, #1113 <https://github.com/fmtlib/fmt/pull/1113>, #1114 <https://github.com/fmtlib/fmt/pull/1114>, #1146 <https://github.com/fmtlib/fmt/issues/1146>, #1180 <https://github.com/fmtlib/fmt/issues/1180>, #1250 <https://github.com/fmtlib/fmt/pull/1250>, #1252 <https://github.com/fmtlib/fmt/pull/1252>, #1265 <https://github.com/fmtlib/fmt/pull/1265>). Thanks @mikelui (Michael Lui) <https://github.com/mikelui>, @foonathan (Jonathan Mรผller) <https://github.com/foonathan>, @BillyDonahue (Billy Donahue) <https://github.com/BillyDonahue>, @jwakely (Jonathan Wakely) <https://github.com/jwakely>, @kaisbe (Kais Ben Salah) <https://github.com/kaisbe>, @sdebionne (Samuel Debionne) <https://github.com/sdebionne>.

    • ๐Ÿ›  Fixed ambiguous formatter specialization in fmt/ranges.h (#1123 <https://github.com/fmtlib/fmt/issues/1123>_).

    • ๐Ÿ›  Fixed formatting of a non-empty std::filesystem::path which is an infinitely deep range of its components (#1268 <https://github.com/fmtlib/fmt/issues/1268>_).

    • ๐Ÿ›  Fixed handling of general output iterators when formatting characters (#1056 <https://github.com/fmtlib/fmt/issues/1056>, #1058 <https://github.com/fmtlib/fmt/pull/1058>). Thanks @abolz (Alexander Bolz) <https://github.com/abolz>_.

    • ๐Ÿ›  Fixed handling of output iterators in formatter specialization for ranges (#1064 <https://github.com/fmtlib/fmt/issues/1064>_).

    • ๐Ÿ›  Fixed handling of exotic character types (#1188 <https://github.com/fmtlib/fmt/issues/1188>_).

    • Made chrono formatting work with exceptions disabled (#1062 <https://github.com/fmtlib/fmt/issues/1062>_).

    • ๐Ÿ›  Fixed DLL visibility issues (#1134 <https://github.com/fmtlib/fmt/pull/1134>, #1147 <https://github.com/fmtlib/fmt/pull/1147>). Thanks @denchat <https://github.com/denchat>_.

    • Disabled the use of UDL template extension on GCC 9 (#1148 <https://github.com/fmtlib/fmt/issues/1148>_).

    • โœ‚ Removed misplaced format compile-time checks from printf (#1173 <https://github.com/fmtlib/fmt/issues/1173>_).

    • ๐Ÿ›  Fixed issues in the experimental floating-point formatter (#1072 <https://github.com/fmtlib/fmt/issues/1072>, #1129 <https://github.com/fmtlib/fmt/issues/1129>, #1153 <https://github.com/fmtlib/fmt/issues/1153>, #1155 <https://github.com/fmtlib/fmt/pull/1155>, #1210 <https://github.com/fmtlib/fmt/issues/1210>, #1222 <https://github.com/fmtlib/fmt/issues/1222>). Thanks @alabuzhev (Alex Alabuzhev) <https://github.com/alabuzhev>_.

    • ๐Ÿ›  Fixed bugs discovered by fuzzing or during fuzzing integration (#1124 <https://github.com/fmtlib/fmt/issues/1124>, #1127 <https://github.com/fmtlib/fmt/issues/1127>, #1132 <https://github.com/fmtlib/fmt/issues/1132>, #1135 <https://github.com/fmtlib/fmt/pull/1135>, #1136 <https://github.com/fmtlib/fmt/issues/1136>, #1141 <https://github.com/fmtlib/fmt/issues/1141>, #1142 <https://github.com/fmtlib/fmt/issues/1142>, #1178 <https://github.com/fmtlib/fmt/issues/1178>, #1179 <https://github.com/fmtlib/fmt/issues/1179>, #1194 <https://github.com/fmtlib/fmt/issues/1194>). Thanks @pauldreik (Paul Dreik) <https://github.com/pauldreik>_.

    • ๐Ÿ›  Fixed building tests on FreeBSD and Hurd (#1043 <https://github.com/fmtlib/fmt/issues/1043>). Thanks @jackyf (Eugene V. Lyubimkin) <https://github.com/jackyf>.

    • ๐Ÿ›  Fixed various warnings and compilation issues (#998 <https://github.com/fmtlib/fmt/pull/998>, #1006 <https://github.com/fmtlib/fmt/pull/1006>, #1008 <https://github.com/fmtlib/fmt/issues/1008>, #1011 <https://github.com/fmtlib/fmt/issues/1011>, #1025 <https://github.com/fmtlib/fmt/issues/1025>, #1027 <https://github.com/fmtlib/fmt/pull/1027>, #1028 <https://github.com/fmtlib/fmt/pull/1028>, #1029 <https://github.com/fmtlib/fmt/pull/1029>, #1030 <https://github.com/fmtlib/fmt/pull/1030>, #1031 <https://github.com/fmtlib/fmt/pull/1031>, #1054 <https://github.com/fmtlib/fmt/pull/1054>, #1063 <https://github.com/fmtlib/fmt/issues/1063>, #1068 <https://github.com/fmtlib/fmt/pull/1068>, #1074 <https://github.com/fmtlib/fmt/pull/1074>, #1075 <https://github.com/fmtlib/fmt/pull/1075>, #1079 <https://github.com/fmtlib/fmt/pull/1079>, #1086 <https://github.com/fmtlib/fmt/pull/1086>, #1088 <https://github.com/fmtlib/fmt/issues/1088>, #1089 <https://github.com/fmtlib/fmt/pull/1089>, #1094 <https://github.com/fmtlib/fmt/pull/1094>, #1101 <https://github.com/fmtlib/fmt/issues/1101>, #1102 <https://github.com/fmtlib/fmt/pull/1102>, #1105 <https://github.com/fmtlib/fmt/issues/1105>, #1107 <https://github.com/fmtlib/fmt/pull/1107>, #1115 <https://github.com/fmtlib/fmt/issues/1115>, #1117 <https://github.com/fmtlib/fmt/issues/1117>, #1118 <https://github.com/fmtlib/fmt/issues/1118>, #1120 <https://github.com/fmtlib/fmt/issues/1120>, #1123 <https://github.com/fmtlib/fmt/issues/1123>, #1139 <https://github.com/fmtlib/fmt/pull/1139>, #1140 <https://github.com/fmtlib/fmt/issues/1140>, #1143 <https://github.com/fmtlib/fmt/issues/1143>, #1144 <https://github.com/fmtlib/fmt/pull/1144>, #1150 <https://github.com/fmtlib/fmt/pull/1150>, #1151 <https://github.com/fmtlib/fmt/pull/1151>, #1152 <https://github.com/fmtlib/fmt/issues/1152>, #1154 <https://github.com/fmtlib/fmt/issues/1154>, #1156 <https://github.com/fmtlib/fmt/issues/1156>, #1159 <https://github.com/fmtlib/fmt/pull/1159>, #1175 <https://github.com/fmtlib/fmt/issues/1175>, #1181 <https://github.com/fmtlib/fmt/issues/1181>, #1186 <https://github.com/fmtlib/fmt/issues/1186>, #1187 <https://github.com/fmtlib/fmt/pull/1187>, #1191 <https://github.com/fmtlib/fmt/pull/1191>, #1197 <https://github.com/fmtlib/fmt/issues/1197>, #1200 <https://github.com/fmtlib/fmt/issues/1200>, #1203 <https://github.com/fmtlib/fmt/issues/1203>, #1205 <https://github.com/fmtlib/fmt/issues/1205>, #1206 <https://github.com/fmtlib/fmt/pull/1206>, #1213 <https://github.com/fmtlib/fmt/issues/1213>, #1214 <https://github.com/fmtlib/fmt/issues/1214>, #1217 <https://github.com/fmtlib/fmt/pull/1217>, #1228 <https://github.com/fmtlib/fmt/issues/1228>, #1230 <https://github.com/fmtlib/fmt/pull/1230>, #1232 <https://github.com/fmtlib/fmt/issues/1232>, #1235 <https://github.com/fmtlib/fmt/pull/1235>, #1236 <https://github.com/fmtlib/fmt/pull/1236>, #1240 <https://github.com/fmtlib/fmt/issues/1240>). Thanks @DanielaE (Daniela Engert) <https://github.com/DanielaE>, @mwinterb <https://github.com/mwinterb>, @eliaskosunen (Elias Kosunen) <https://github.com/eliaskosunen>, @morinmorin <https://github.com/morinmorin>, @ricco19 (Brian Ricciardelli) <https://github.com/ricco19>, @waywardmonkeys (Bruce Mitchener) <https://github.com/waywardmonkeys>, @chronoxor (Ivan Shynkarenka) <https://github.com/chronoxor>, @remyabel <https://github.com/remyabel>, @pauldreik (Paul Dreik) <https://github.com/pauldreik>, @gsjaardema (Greg Sjaardema) <https://github.com/gsjaardema>, @rcane (Ronny Krรผger) <https://github.com/rcane>, @mocabe <https://github.com/mocabe>, @denchat <https://github.com/denchat>, @cjdb (Christopher Di Bella) <https://github.com/cjdb>, @HazardyKnusperkeks (Bjรถrn Schรคpers) <https://github.com/HazardyKnusperkeks>, @vedranmiletic (Vedran Miletiฤ‡) <https://github.com/vedranmiletic>, @jackoalan (Jack Andersen) <https://github.com/jackoalan>, @DaanDeMeyer (Daan De Meyer) <https://github.com/DaanDeMeyer>, @starkmapper (Mark Stapper) <https://github.com/starkmapper>_.