4 ms·
Nope. The equivalent in C++ is not std::cout << "hello world" << std::endl; but is, rather, std::cout << "hello world\n"; The std::endl doesn't just print a
by not2b 4y ago
Nope. The equivalent in C++ is not
std::cout << "hello world" << std::endl;
but is, rather,
std::cout << "hello world\n";
The std::endl doesn't just print an '\n' character, it also does a buffer flush.
- CoastalCoder 4y agoI'm not sure how we define "equivalent" here. If we're talking about the majority of C++ code I see in commercial systems, then `std::endl` is definitely used more than `'\n'` to emit a line-break. (std::endl also has the benefit of doing the right thing across different operating systems)
- plorkyeran 4y agostd::cout << "\n"; will emit \r\n on Windows by default.
- CoastalCoder 4y agoI wouldn't have expected that. Is it compiler-specific? Because it sounds totally broken, but maybe the language spec covers this and I didn't realize it.
- plorkyeran 4y agoSimilar to FTP, iostreams have a concept of text mode and binary mode for streams. In text mode, newlines are translated and in binary mode they are not. Which mode std::cout is in by default is implementation-defined just to be obnoxious. VC++ defaults to text mode.
- deleted 4y ago[deleted]
- not2b 4y agoIn the code I write and review I care about the buffer-flush semantics and will revise, or ask for a revision, if excessive flushing will result from too many std::endl uses when generating large volumes of output. It isn't just an academic point; you might want to look at that code to see if std::endl is justified. It is appropriate if you want to guarantee that a given record reaches the output file quickly, and if not much is written it's a don't-care.
- CoastalCoder 4y agoThat's my policy too. I was pointing out that what we consider best practice isn't necessarily common practice. So the original story might benefit from additional nuance / distinctions.
- pjmlp 4y agoBeing pedantic, the equivalent C++ is still his printf example, as it is valid C++ code. One isn't forced to use iostreams, format, or now the new kid in town, print.