Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

c++ - Is std::cout buffered?

Just reading an old but interesting article by "Scott Meyers"

http://aristeia.com/Papers/C++ReportColumns/novdec95.pdf

Basically it is about preferring to use ' ' over std::endl (which I agree with and have used the same augment for years).

BUT the last section indicates this was not included in his book because the whole thing was rendered moot because of two points:

  1. std::cout was not buffered.
  2. The state of ios::unitbuf on std::cout is not explicitly defined (thus implementation dependent).

I did a quick look but could not find an explicit standards reference for 1 was true. Is std::cout unbuffered contrary to what I have always understood?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, it's buffered:

C++11 27.4.2 [narrow.stream.objects]/3 : The object cout controls output to a stream buffer associated with the object stdout

The article refers to a 1995 draft version of what became the C++98 standard. I've no idea whether or not that might have said something different.

As for point 2, unitbuf is initially false on all streams (specified by the post-conditions of the basic_ios constructor), except for cerr and wcerr which explicitly specify otherwise. Again, that may well be different in the ancient draft being referred to.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...