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.
48 0 Replystd::endl provides zero portability benefits. C++ does have a portable newline abstraction, but it is called
\n
, not endl.35 0 ReplyThank you two for demonstrating the image in the post so well.
48 0 ReplyNo, there's no guarantee that in every context \n is translated portably.
7 0 Reply
They aren't the same thing so the comparison is weird.
endl
has aflush
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.
21 0 Replybut the goal should always be to write as optimal as possible.
Within reason.
Over optimization is a curse on getting done.
3 0 ReplyWho in the hell is using iostreams in an RTOS
3 0 ReplySometimes you work in a codebase that was decided on by others for reasons you don't know.
2 0 ReplySeveral. Probably dozens
2 0 Reply
\n, because I ordered a newline, not a flush.
19 0 ReplyWhat the heck is endl???
15 0 Replystd::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 preferred39 0 ReplyInstead of this:
cout << "Hello world.\n";
You can do this:
cout << "Hello world." << endl;
10 0 ReplyThe fact that you used the namespace for
cout
but not forendl
inordinately bothers me6 0 Replysomething has replaced your lessthan signs with their HTML counterparts. such a silly thing to do in a code block
1 0 Reply
Boy am I glad I don’t do C++ anymore. That string handling with the overloaded bitshift operator was wild.
11 0 ReplyAlternatively:
https://en.cppreference.com/w/cpp/io/manip/endl
p.s. The site isn't entirely mobile friendly
(I'm a cppref lover tbh)
2 0 Reply
From memory it's a way to declare a line ending after your string.
3 0 ReplyGod bless your soul.
2 0 Reply
#define endl "\n"
15 0 Reply\r\n gang
14 0 ReplyGet out
19 0 ReplyYeah
\r
gang4lyfe4 0 Reply
Are you a modem by any chance?
2 0 ReplyNo, I'm a teapot
1 0 Reply
Environment.NewLine()
13 0 Reply/* I'm new to this language so just imagine there is a new line here when it prints: */
9 0 Reply
os.linesep
Lol jk none of my stuff runs on Windows anyway
11 0 ReplyI'm on side
\PHP_EOL
9 0 ReplyI like that you added the absolute namespace identifier or whatever its called
2 0 ReplyI don't always namespace but when I do I fully qualify.
1 0 Reply
If you write to a text (as opposed to binary) stream, \n produces \n or \r\n (or \r if old enough) depending on platform just fine.
Nobody should be using C++ anyway, but plenty of languages have silly system newline constants, which do nothing useful.
5 0 ReplyWhy should no one be using c++?
4 0 Reply
Rebel side
\0
5 0 Reply5 0 ReplyEnvironment.NewLine might exist in C#
5 0 ReplyIt might do. I encountered it last week as I needed it for a powershell script. So it exists in that at least
2 0 ReplyMicrosoft really creating the problem and then forcing you to use their solution.
1 0 Reply
Well, Java has System.lineSeparator so, maybe no?
4 0 ReplyvbCrLf
3 0 ReplyEndl is faster to type
2 0 Reply