4 ms·
I know this is a just for fun post, but C++ engineers haven't been especially happy with iostreams. {fmt} and std::format are more modern alternatives. And use
by humanrebar 4y ago
I know this is a just for fun post, but C++ engineers haven't been especially happy with iostreams. {fmt} and std::format are more modern alternatives.
And use of std::endl is widely considered an antipattern. Use of '\n' is always better for printing new lines.
- jcelerier 4y ago> And use of std::endl is widely considered an antipattern. Use of '\n' is always better for printing new lines. people say that and then I loose half an hour trying to understand where my code is going wrong because my terminal did not flush when trying to debug log
- humanrebar 4y agoWhen I want to flush, I use std::flush. Nothing about "endl" screams "this flushes", which is why nobody uses it. Also, flushing on every newline is a needless pessimization. Not that performance is everything, but it doesn't buy you much here. endl just saves you from getting out of the habit of using endl, really.
- Kranar 4y ago>Nothing about "endl" screams "this flushes", which is why nobody uses it. Are you seriously suggesting no one uses std::endl? A simple search on Github actually reveals the opposite... almost everyone uses std::endl and almost no one uses std::flush (94% use std::endl, and 6% use std::flush). I am also in the std::endl camp and find it absurd when people bike shed over it. It's the most intuitive/least surprising behavior (based on Stack overflow questions) and how almost all other mainstream languages behave (flush on new line), and as such it should be the default choice. The use of ... << '\n' << std::flush is best when you want to explicitly call out that behavior because of performance reasons or some other exceptional circumstance.
- UncleOxidant 4y ago> Also, flushing on every newline is a needless pessimization. Maybe, but when you're debugging it's essential. Those debugging cout's aren't going to be there in production.
- leni536 4y agocerr should flush on \n too by default, AFAIK.
- gusfrehse 4y agoJust use cerr for debugging. It is not buffered so it always outuputs, even in unexpected crashes.
- jcelerier 4y agoI wish it would but this is just not what I observe here, I have to flush explicitly to get things to show on the console
- int_19h 4y agostd::cerr being unbuffered is one of its defining features. If you're not seeing that, either you (or some dependency you use) disables it, or it's a bug in your C++ stdlib.
- shadowfox 4y ago> Use of '\n' is always better for printing new lines. Does this do the right thing on Windows?
- afranchuk 4y agoYes! For the same reason it does the right thing in C APIs. Which many people don't realize (understandably) until they read the fine print in docs. If you read the specification of std::endl, it simply sends a "\n" and flushes.
- duped 4y agoUnless you explicitly set O_BINARY on stdout/stderr then LF is converted to CRLF, aka "text mode" vs "binary mode" for files.