C++ Question on using endl or newline
I've seen a number of documents and code that prefers to use the newline
character ("\n" or '\n') rather than std::endl
In the general case, the issue is that endl forces a buffer flush where '\n'
does not, and therefore using the newline is more efficient. However, using
endl is more portable. Either way works fine on Linux. Comments...
--
Jerry Feldman
On Monday 03 April 2006 17:18, Jerry Feldman wrote:
I've seen a number of documents and code that prefers to use the newline character ("\n" or '\n') rather than std::endl In the general case, the issue is that endl forces a buffer flush where '\n' does not, and therefore using the newline is more efficient. However, using endl is more portable. Either way works fine on Linux. Comments...
It's not only portability, it's also about buffer flushing:
http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
OTOH this might be a reason to advise against std::endl, too:
http://llvm.org/docs/CodingStandards.html#hl_avoidendl
CU
--
Stefan Hundhammer
On Monday 03 April 2006 11:58 am, Stefan Hundhammer wrote:
On Monday 03 April 2006 17:18, Jerry Feldman wrote:
I've seen a number of documents and code that prefers to use the newline character ("\n" or '\n') rather than std::endl In the general case, the issue is that endl forces a buffer flush where '\n' does not, and therefore using the newline is more efficient. However, using endl is more portable. Either way works fine on Linux. Comments...
It's not only portability, it's also about buffer flushing:
http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
OTOH this might be a reason to advise against std::endl, too:
http://llvm.org/docs/CodingStandards.html#hl_avoidendl I was looking for something like this that is specific and to the point. Some C++ texts teach std::endl, and others teach that using the newline characters. I knew that endl did the buffer flush. The main reason I asked is to get some good definitive answers since I do occasionally teach C++ (although I am primarily a C programmer). -- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Monday, 3. April 2006 17:18, Jerry Feldman wrote:
I've seen a number of documents and code that prefers to use the newline character ("\n" or '\n') rather than std::endl In the general case, the issue is that endl forces a buffer flush where '\n' does not, and therefore using the newline is more efficient. However, using endl is more portable. Either way works fine on Linux. Comments... --
I think the choice is purely if you would like the buffer flush or not. The is no portability issue with '\n':
In the ISO standard section 27.6.2.7 std::endl is defined as
namespace std {
template
participants (3)
-
Jerry Feldman
-
Michael Stevens
-
Stefan Hundhammer