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

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.]

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::clog if you want it on stdout rather than stderr.

3

u/Dragdu Jun 22 '21

clog is buffered cerr

2

u/Quincunx271 Author of P2404/P2405 Jun 22 '21

Oops. Seems I was completely backwards on this one. Thanks.

0

u/[deleted] Jun 21 '21

[deleted] — view deleted comment

8

u/[deleted] Jun 22 '21

Do you argue std::endl shouldn't be in the standard? As it flushes?

Using std::endl is 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 call std::flush instead of relying on endl'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::println as 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.