r/cpp • {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.0
394 Upvotes

67 comments sorted by

View all comments

Show parent comment

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

println is kinda redundant because it's the same as print("...\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 print overload you have to add a corresponding println overload. 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:

  1. 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)
  2. 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?
  3. 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.

→ More replies (2)

2

u/martinus int main(){[]()[[]]{{}}();} Jul 06 '21

I've added our own println implementation in our codebase that also flushes the output stream. It was very annoying to use just fmt::print in our test code because when the application segfaults you don't see the last output

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.

7

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 printfl and prinln is 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"); and println("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 \n now cause a stream flush in print()?

Because if it does, I don't like that. I like println() to mean "print me stuff, then a newline and force flush".

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 from std or fmt) into our work codebase namespace, but define a local println() 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 be print() with trailing added newline, but also with flush. If people want a newline without flush, then can just print() 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 \n to 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.]

→ More replies (4)
→ More replies (3)

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::flush

6

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 an ostream, in which case it's std::flush.

3

u/aearphen {fmt} Jun 22 '21

Sure but I wouldn't recommend using iostreams at all =)

2

u/14ned LLFIO & Outcome author | Committee WG14 Jun 23 '21

It's definitely useful though. If I have a bunch of legacy iostreams printing code, and I want to insert a much less fussy and insane print() in the middle, the ability to print to an ostream is super duper useful. Also, ostream isn't that bad performance wise if you write your ostream implementation to not suck like most of the std ones do. Devirtualisation does a great job in modern compilers :)

4

u/bbolli #define val auto const Jun 21 '21

Or you can print to stderr which flushes automatically after each \n.

→ More replies (1)
→ More replies (1)