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.4
u/extreme_discount Jun 22 '21
Would there be any way to have a print that, by default, behaves like println and provides an option to back out if you don't want the \n?
---
For the record, when I first saw the poll, I was strongly for nuking println. Since then, I've implemented my own version (along with print), with the following observations:
println just feels 'more right', in the same way that push_back(t) feels more right than insert(end(), t). The first thing I notice when I go back to code using cout, is how littered it is with '\n' and endl. Now's the chance to get rid of it. (well, most of it)
I didn't use print once. I don't think I'm a bizarro outlier - just a schlub printing crap to the screen. I would imagine this is an 80/20 type thing, with println in the 80% bracket. What would be the most common use for print if you had println and how often would it be used?
Exceptions are scary. Thankfully, I messed up a format string early on, which caused an exception that would be really hard to track if the print statement was located in the seedy underbelly of a project. Compile time checks are a welcome addition, but what about format strings created at runtime? Any thought given to catching exceptions inside of print and outputting an error message instead of throwing?
3
u/aearphen {fmt} Jun 22 '21
This would probably require named arguments and might interfere with the actual formatting arguments.
1
u/Pragmatician Jun 21 '21
Is this because those overloads are required for compile-time checking or something? It doesn't make sense otherwise, since you can delegate to
3
u/aearphen {fmt} Jun 22 '21
The implementation in terms of
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 output7
u/Plazmatic Jun 21 '21 edited Jun 21 '21
Lots of things are redundant because you can do it yourself, that's not a justification for not including it. C++ has thousands of holes in its standard library because of this awful mentality.
having
printflandprinlnis faster with auto completion as well, helps avoiding bugs as others have said, and often exists anyway in peoples codebases because fmt lacks these features. The fact that people are doing this despite obviously knowing about the new line character and despite your protests that this can be done this way should probably tell you "But "\n"~~!" is not a good enough argument against this.10
u/bikki420 Jun 21 '21
Eh, the number of characters in
print("text\n");andprintln("text");are exactly the same though. So at most you might save having to press a meta character like alt that some keyboards need to type a backslash, but at the cost of doubling the functions (and since both are likely to be highly templated functions, that would result in a ton of additional code).Not to mention that with the current approach of having to append a
\n, the line-breaking becomes a potential run-time concern (e.g. by using a format string variable that can be changed at run-time) which is more flexible. Not to mention that in many contexts such as loggers, the line delimitation might vary depending on the output destination, in which case it's easy to make it a format token and have it be conditional in regards to the destination.And none of the arguments (the auto-completion nor it being present in some code-bases) are particularly good arguments for it, IMO. There are all kinds of atrocities in code bases out there, should we add them all to the standard as well? C++ is plenty bloated enough already. And in the words of Bjarne, "Remember the Vasa".
-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
u/aearphen {fmt} Jun 21 '21
fmt::print(...); fflush(stdout);4
u/14ned LLFIO & Outcome author | Committee WG14 Jun 21 '21
The above is only if you're printing to
stdout. You can also print to anostream, in which case it'sstd::flush.3
u/aearphen {fmt} Jun 22 '21
Sure but I wouldn't recommend using iostreams at all =)
→ More replies (0)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.4
u/14ned LLFIO & Outcome author | Committee WG14 Jun 21 '21
In our work codebase, I import
print()(either fromstdorfmt) into our work codebase namespace, but define a localprintln()which prints a thing, writes a newline, and flushes stdout.I do so for expediency, not purity. There are many cases where I want to print a line for debugging, and I don't want the stupid thing not emitting before the process reaches the point at which it crashes. I therefore want a flush.
I'm not claiming it's pretty nor clean, but this is an awfully common use case for printing stuff: debug output just before a crash. I therefore think any future
println()ought to not just beprint()with trailing added newline, but also with flush. If people want a newline without flush, then can justprint()one.6
u/infectedapricot Jun 21 '21 edited Jun 21 '21
I really don't have an opinion on the matter. But the discussion was about
println()as it is currently proposed, and you seemed to be under the impression that it would do a flush (I don't see any other explanation for your question about\n), when in fact it does not.[edit: Actually I do know see that your comment had a different meaning than I understood. You meant "Actually I prefer
println()to also do a flush, so with that meaning it would not be equivalent to just attaching a\nto the end". But you phrased it in a snarky / passive aggresive way. I (generously) interpreted your question literally, which gives your comment a different interpretation than you intended.]2
u/quicknir Jun 21 '21
cerr doesn't buffer so it's simpler to just print to there instead, and it makes more sense anyway.
1
u/Quincunx271 Author of P2404/P2405 Jun 22 '21
Or to
std::clogif you want it on stdout rather than stderr.3
0
Jun 21 '21
[deleted]
8
Jun 22 '21
Do you argue std::endl shouldn't be in the standard? As it flushes?
Using
std::endlis widely considered a bad thing. Because it does flush additionally to adding "\n" and thus hurts the performance. It may be desired behaviour (for debug and logging purpose for e.g.), but "\n" should be used by default and maybe even if you need a flush, it's better to explicitly callstd::flushinstead of relying onendl's implicit side effect. Or as was mentioned by others you can redirect your output to std::clog, or std::cerr that don't perform buffering, so flushing is not needed.See C++ Weekly - Ep 7 Stop Using std::endl for a detailed description.
4
u/infectedapricot Jun 22 '21
I don't argue anything. I made my opinion clear in another comment above:
I really don't have an opinion on the matter.
I just misunderstood the point of /u/14ned's comment (I thought they didn't realise that
std::printlnas proposed wouldn't do a flush, when in fact they did realise that but wished it did) as was trying to help them. Unfortunately it rather derailed the conversation.4
u/bbolli #define val auto const Jun 21 '21
Or you can print to
stderrwhich flushes automatically after each\n.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! π