166
top 50 comments
sorted by: hot top controversial new old
[-] SpaceNoodle@lemmy.world 46 points 1 month ago* (last edited 1 month ago)

If I'm writing C++, I'm usually optimizing for portability over performance, in which case I would prefer std::endl as it would yield the best results regardless of platform; it also keeps the end-of-line character out of other strings, making code just a little cleaner.

\n is for when I'm done pretending that anything that isn't Unix-like is OK, or I'm counting the cycles of every branch instruction.

[-] barubary@infosec.exchange 33 points 1 month ago

std::endl provides zero portability benefits. C++ does have a portable newline abstraction, but it is called \n, not endl.

[-] Albbi@lemmy.ca 49 points 1 month ago

Thank you two for demonstrating the image in the post so well.

[-] SpaceNoodle@lemmy.world 7 points 1 month ago

No, there's no guarantee that in every context \n is translated portably.

[-] barubary@infosec.exchange 16 points 1 month ago

The same is true of std::endl. std::endl is simply defined as << '\n' << std::flush; nothing more, nothing less. In all cases where endl gives you a "properly translated" newline, so does \n.

[-] SpaceNoodle@lemmy.world 7 points 1 month ago

Ahhh, I see. Looks like the magic happens somewhere further down in iostream.

[-] AnyOldName3@lemmy.world 4 points 1 month ago

It's controlled by whether the stream's opened in text mode or binary mode. On Unix, they're the same, but on Windows, text mode has line ending conversion.

[-] zenforyen@feddit.org 3 points 1 month ago

Yeah it's an artificial dichotomy based on a popular misconception of what std::endl is and how \n is interpreted.

Ultimately it does not ask about line endings, but about flushing, which is a completely orthogonal question.

[-] jecxjo@midwest.social 22 points 1 month ago

They aren't the same thing so the comparison is weird.

endl has a flush which is important when doing something like embedded work or RTOS development. If i was doing multiple lines they all were \n until the last line when i actually want to push the buffer.

Obviously depending on the tuning of the compiler's optimization multiple flushes could be reduced but the goal should always be to write as optimal as possible.

[-] aport@programming.dev 3 points 1 month ago

Who in the hell is using iostreams in an RTOS

[-] KeenFlame@feddit.nu 2 points 1 month ago

Several. Probably dozens

[-] jecxjo@midwest.social 2 points 1 month ago

Sometimes you work in a codebase that was decided on by others for reasons you don't know.

[-] lud@lemm.ee 3 points 1 month ago

but the goal should always be to write as optimal as possible.

Within reason.

Over optimization is a curse on getting done.

[-] UpperBroccoli@lemmy.blahaj.zone 20 points 1 month ago

\n, because I ordered a newline, not a flush.

[-] unlawfulbooger@lemmy.blahaj.zone 17 points 1 month ago
[-] vapeloki@lemmy.world 40 points 1 month ago

std::endl is used in output streams in C++ to end the line, using the os specific line termination sequence, and flush the buffer.

The later one is a performance issue in many cases, why the use of "\n" is considered preferred

[-] xigoi@lemmy.sdf.org 2 points 1 month ago

Don’t most terminals flush the buffer on newline anyway?

[-] ClemaX@lemm.ee 8 points 1 month ago

It is the stream itself that is buffered, so the terminal does not handle the contents until the stream is flushed.

[-] vapeloki@lemmy.world 2 points 1 month ago

Maybe, but there is the internal buffer. Also, most I/O happens in files not consoles

[-] hellfire103@lemmy.ca 10 points 1 month ago* (last edited 1 month ago)

Instead of this:

std::cout << "Hello world.\n";

You can do this:

std::cout << "Hello world." << endl;
[-] Daedskin@lemm.ee 6 points 1 month ago

The fact that you used the namespace for cout but not for endl inordinately bothers me

load more comments (4 replies)
[-] gon@lemm.ee 7 points 1 month ago
[-] unlawfulbooger@lemmy.blahaj.zone 11 points 1 month ago

Boy am I glad I don’t do C++ anymore. That string handling with the overloaded bitshift operator was wild.

load more comments (2 replies)
[-] SqueakyBeaver@lemmy.blahaj.zone 2 points 1 month ago

Alternatively:

https://en.cppreference.com/w/cpp/io/manip/endl

p.s. The site isn't entirely mobile friendly

(I'm a cppref lover tbh)

[-] allywilson@lemmy.ml 3 points 1 month ago

From memory it's a way to declare a line ending after your string.

[-] jaybone@lemmy.zip 2 points 1 month ago

God bless your soul.

[-] GideonBear@lemmy.ml 16 points 1 month ago

#define endl "\n"

[-] scrubbles@poptalk.scrubbles.tech 12 points 1 month ago
[-] tourist@lemmy.world 10 points 1 month ago
/* I'm new to this language so just imagine there is a new line here when it prints: */
[-] lengau@midwest.social 11 points 1 month ago

os.linesep

Lol jk none of my stuff runs on Windows anyway

[-] besselj@lemmy.ca 10 points 1 month ago
[-] SpaceNoodle@lemmy.world 20 points 1 month ago
[-] xmunk@sh.itjust.works 3 points 1 month ago

Yeah \r gang4lyfe

[-] aport@programming.dev 2 points 1 month ago

Are you a modem by any chance?

load more comments (1 replies)
[-] xmunk@sh.itjust.works 9 points 1 month ago
[-] grandel@lemmy.ml 2 points 1 month ago

I like that you added the absolute namespace identifier or whatever its called

load more comments (1 replies)
[-] nope@jlai.lu 6 points 1 month ago

Environment.NewLine might exist in C#

[-] MrScottyTay@sh.itjust.works 2 points 1 month ago

It might do. I encountered it last week as I needed it for a powershell script. So it exists in that at least

load more comments (1 replies)
[-] pewpew@feddit.it 5 points 1 month ago
[-] savvywolf@pawb.social 5 points 1 month ago
[-] lnxtx@feddit.nl 4 points 1 month ago

Rebel side \0

[-] bramen49@lemmy.ml 4 points 1 month ago

Well, Java has System.lineSeparator so, maybe no?

[-] dave@feddit.uk 3 points 1 month ago
[-] RandomVideos@programming.dev 2 points 1 month ago

Endl is faster to type

load more comments
view more: next ›
this post was submitted on 05 Apr 2025
166 points (95.6% liked)

Programmer Humor

35551 readers
54 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS