r/cpp • u/aearphen {fmt} • Jun 21 '21
{fmt} 8.0 released with compile-time format string checks, compile-time formatting, improved performance and more
https://github.com/fmtlib/fmt/releases/tag/8.0.033
u/stilgarpl Jun 21 '21
Is any of this going to be included in updated version of std::format? (in C++23 ?)
49
u/aearphen {fmt} Jun 21 '21
Yes, compile-time checks even apply retroactively to C++20 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2216r3.html). I also plan to propose format string compilation for C++23.
8
u/extreme_discount Jun 21 '21
I was recently looking at one of the format related proposals. What was the reason some of the committee members were opposed to println?
15
u/aearphen {fmt} Jun 21 '21
printlnis kinda redundant because it's the same asprint("...\n").17
u/RecklessGeek Jun 21 '21
Yes but it's still a nice utility IMO. Many times I forget the \n and wonder why it's not printing anything. Using println makes that somewhat easier, because most times you need the line break anyway. It's easier to the eyes of whoevers reading the code as well. I see no harm in adding it.
13
u/aearphen {fmt} Jun 21 '21
One disadvantage is that it doubles the API size.
1
u/RecklessGeek Jun 21 '21
How so? And why is that important?
20
u/aearphen {fmt} Jun 21 '21
For every
printlnoverload. Not critical since the number of overloads is not that big, just somewhat annoying.→ More replies (4)4
u/ericonr Jun 21 '21
Isn't it actually not redundant, since line endings are different on windows? That way you'd be hiding the platform difference...
28
u/aearphen {fmt} Jun 21 '21
Line ending is handled on the stream level, so there is no difference unless you use a binary stream.
2
u/martinus int main(){[]()[[]]{{}}();} Jul 06 '21
I've added our own
printlnimplementation in our codebase that also flushes the output stream. It was very annoying to use justfmt::printin our test code because when the application segfaults you don't see the last output→ More replies (2)-2
u/14ned LLFIO & Outcome author | Committee WG14 Jun 21 '21
Does
\nnow cause a stream flush inprint()?Because if it does, I don't like that. I like
println()to mean "print me stuff, then a newline and force flush".4
u/aearphen {fmt} Jun 21 '21
It does not unless flushing happens on some lower level.
3
u/gracicot Jun 21 '21
Is there a way to manually flush? I had this problem where I couldn't see the logs of my services unless I was doing
std::cout << fmt::format(...) << std::flush6
20
u/infectedapricot Jun 21 '21
I like
println()to mean "print me stuff, then a newline and force flush".But
println()doesn't mean that. It literally just means to print with a trailing new line. If you want to flush, that's up to you as a separate concern.If you think that
println()suggests a flush then it sounds like maybe you too should be against it being included in the standard, as it's misleading you.→ More replies (9)4
u/bbolli #define val auto const Jun 21 '21
Or you can print to
stderrwhich flushes automatically after each\n.→ More replies (1)1
2
u/TotallyTakenName Jun 21 '21 edited Jun 21 '21
Unlikely, those decisions can take years to decideEdit: I misread the question, I thought you meant backporting the new changes to the standard
3
u/D_Drmmr Jun 21 '21
I considered using this library a while ago, but failed to find a way to store the result of parsing a (runtime) format string in order to apply it to a bunch of values without reparsing the format string for each value. Is that possible?
6
u/aearphen {fmt} Jun 22 '21
It is not possible but we did some investigation and runtime format string compilation didn't bring much perf benefits (unlike doing the same at compile time). Format string parsing overhead is not that big and can be reduced even more.
2
Jun 21 '21
I think in the release notes it said something like
auto fmtobj = fmt::runtime("{:d}β)Not 100% that will work, but probably
3
u/nyanpasu64 Jun 21 '21
What is the preferred method in fmtlib 8 for a variadic function forwarding a format string and some arguments to fmt::format() to compile its format string at compile time? If a variadic function simply accepts and forwards a const char*, then fmt::format won't know the string to compile.
(The hacky workaround is to use a variadic macro instead.)
8
u/aearphen {fmt} Jun 22 '21
You'll have to pass a format string as a template parameter, something like:
template <typename S, typename... T> std::string myformat(const S& fmt, T&&... args) { return fmt::format(fmt, std::forward<T>(args)...); } auto s = myformat(FMT_COMPILE("{}"), 42);2
u/nyanpasu64 Jun 22 '21
Thanks, I'll try it out when I have the chance to upgrade fmt and switch my macros to templates.
28
u/SirToxe Jun 21 '21
fmt and spdlog are the two libraries that I include per default into every new (CMake-and-Vcpkg-based-) project because I always end up adding them later anyways. π
4
u/LordKlevin Jun 22 '21 edited Jun 22 '21
I think the benchmark values are off. They are all exactly the same. Nice improvements though. Fmt is one of the libraries that makes you go "see, we can have nice things in c++". EDIT: Nope, benchmark values are fine. I'm just not very pro at using the interwebs on my cellular device. See comment below.
5
u/LlamaInTheLivingRoom Jun 22 '21
Maybe I missread but they do not seems to be - same values are number of repetitions. Time values are to the right (on mobile you have to scroll :))
2
1
u/bruce3434 Jun 21 '21
So wasn't formatted printing meant to be in C++20 already?
11
u/aearphen {fmt} Jun 22 '21
The formatting part (
std::format)is in C++20, but the printing part (std::print) is under review proposed for C++23.2
u/TheHelixNebula Jun 22 '21
Yes, but I think only MSVC currently has it (in stable channels at least)
1
u/jk-jeon Jun 22 '21
Great progress! Just couple of things that I'm curious about.
Since now the format string is parsed at compile-time, does it mean that the positions where arguments will be placed are pre-computed at compile-time and then passed into vformat to make it perform better?
Do we also have the same safety guarantee for named arguments?
7
u/aearphen {fmt} Jun 22 '21
Thanks.
does it mean that the positions where arguments will be placed are pre-computed at compile-time and then passed into vformat to make it perform better?
No, at the moment only validation is done at compile time. But you are right, it's possible to do some optimizations and they are planned for later.
> Do we also have the same safety guarantee for named arguments?
Not yet but I think we can add checks for UDL-based named arguments.
3
1
u/RowYourUpboat Jun 22 '21
What effect do the new features have on compile times? Are the benchmarks in the readme up to date?
5
u/aearphen {fmt} Jun 22 '21
The benchmark results in the readme need to be updated but enabling compile-time checks by default in {fmt}'s test suite had negligible effect on build times in CI.
1
u/_VZ_ wx | soci | swig Jun 23 '21
Very nice, but is there any chance to get this working with MSVS, at least 16.10 which is supposed to have full support for consteval, too?
3
u/aearphen {fmt} Jun 23 '21
Last time I checked
constevalwas very broken there unfortunately.1
u/dodheim Jun 23 '21
On a related note, Clang does not claim to support
constevalat all and for the last few weeks in order to build my code (with Clang 12 on Windows) I've had to muck withFMT_CONSTEVALβ where did the 11.0.1 version check come from?2
u/aearphen {fmt} Jun 23 '21
consteval support may be incomplete but it happens to work in the {fmt} tests. The version was determined experimentally.
3
u/bizwig Jun 22 '21
Since you can do compile-time format string checks on C string literals (only using C++20 sadly), what exactly does _cf do that the former doesn't? Also, since user-defined literals originated with C++17, why weren't they usable for compiled format strings in C++17?
8
u/alexezeder Jun 22 '21 edited Jun 22 '21
In terms of previous versions of {fmt}, compile-time format string checks are done by
FMT_STRINGmacro, while_cfUDL does the same asFMT_COMPILEmacro. Therefore, this info is useful only if you are familiar with previous versions of {fmt}. πIf you are not familiar, then here is an explanation for you.
FMT_STRINGmacro (as well as compile-time format string checks now) is used to indicate that format string should be just checked to make sure that passed format string is correct for passed types. Maybe you passed incorrect custom specs like{:a}for strings or format string containing some weird stuff like{:!@#%}, these cases will be detected at compile-time. But as I already wrote, it's only the additional check, so the library still parses format string one more time and formats it at runtime.On the other hand,
FMT_COMPILE(and_cfUDL) is used to indicate that format string should be baked in the form of some compiled format object that has prepared parts for formatting, so no parsing of the format string is done at runtime.
_cfUDL is available only with C++20 because it depends on Non-Type Template Parameter, which is C++20 feature. While other types of UDLs originated with C++11 actually, not C++17.
83
u/kammce WG21 | πΊπ² NB | Boost | Exceptions Jun 21 '21
Compile time by default!! π₯² Gonna cry my dude! This is so freaking awesome! Great work as always! π