Skip to content

Replace '\n' -> std::endl in 'cout' cpp snippet #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2017
Merged

Replace '\n' -> std::endl in 'cout' cpp snippet #12

merged 1 commit into from
Nov 29, 2017

Conversation

rgcv
Copy link
Contributor

@rgcv rgcv commented Nov 26, 2017

Don't inline '\n'.

Right off the bat, it's incompatible with Windows where the EOL is CRLF (\r\n). Using std::endl avoids these kind of issues and nuisances.

EDIT: I did not include it, but I could've bumped the version as well.. However, it does make sense to only do it if the PR gets merged.

@one-harsh one-harsh merged commit 6479295 into one-harsh:master Nov 29, 2017
@jwood13
Copy link
Contributor

jwood13 commented Feb 15, 2018

I'm late to the party, but \n is cross platform provided that you're using a text stream, It's automatically converted when appropriate (http://en.cppreference.com/w/c/io C streams on Windows OS convert \n to \r\n on output, and convert \r\n to \n on input ). std::endl forces a flush of the buffer, which can slow things down depending on the situation.

@rgcv
Copy link
Contributor Author

rgcv commented Feb 16, 2018

@jwood13 Huh, did not know that. Thanks for enlightening me!

However, shouldn't, by default, std::endl be used to terminate a line? Conventionally speaking, inlining either \n or \r\n looks rather.. messy, a defunct artifact of olden code (I've seen \r\n in .NET code and it made me curl up in a ball and cry.. System.Environment.LINE_SEPERATOR if need be!). Say a new hipster OS comes out and \n (or 0x0A) is no longer a line terminator? (jk.. bad joke, no new system wouldn't fail to comply to industry set standards with over a couple decades now)

I do agree: in the case one looks to get every nickle of performance out of C++, std::endl shouldn't be used unless you want to flush the buffer's contents. But is that the main use case? Say, for >90% of people using C++? (EDIT: As I read it back, it probably might be. Why would you use C++ otherwise, besides, ultimately, preferring performance over the overhead other languages impose)

One could argue snippets are for those who needn't write the same line of code for the millionth time since they're knowledgeable enough to that point, meaning getting a bit of a perf bonus just from the snippet would be nice.. 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants