Topic: sync: standard vs Stroustrup
Author: kuehl@horn.fmi.uni-konstanz.de (Dietmar Kuehl)
Date: 1999/05/01 Raw View
Hi,
Michel Michaud (Michel.Michaud@sympatico.ca) wrote:
: Nothing undefined here, from the start to the finish but wait...
: "the characters ... are WRITTEN"? That really looks like a "flush"
: on an OUTPUT stream! So it is really undefined for an istream,
: although the logic of the standard would then be really strange
: (because the conclusion is reached from basic_istream), and
: Dr.Stroustrup would be wrong !?
Note that stream buffers are used for both input and output. The
operation 'sync()' applies to both input and output. The typical use of
sync'ing streams is to flush the output buffer to make sure something
is written. A corresponding use of input is normally not needed but
still possible (it could make sense eg. for a socket connection to make
sure the OS buffer is empty).
: I guess there is something missing... Does anyone has any idea?
You looked at the wrong place... The description for 'basic_streambuf'
describes the intended functionality to be implemented by derived
classes. What is really going on depends on the derived class. For the
two standard classes derived from 'basic_streambuf', namely
'basic_stringbuf' and 'basic_filebuf' (I'm ignoring 'strstreambuf'),
the behavior is defined in the corresponding sections: No special
definition for 'basic_stringbuf', ie. the default implementation is
used (do nothing which is the obvious choice for 'basic_stringbuf') and
flushing the output buffer plus some implementation defined behavior on
the input buffer (my guess: always do nothing special with the input
buffer...).
This issue was discussed at the Dublin meeting: It was brought up as a
potential defect but I convinced the library group that there is
nothing wrong with the description of 'sync()'. For the two standard
classes it is reasonably described and the general intend in the base
class is also reasonably described. It makes no sense to describe
whatever possible behavior 'sync()' can have on strange streams where
the input sequence is changed. This is completely up to the implementer
of the corresponding stream buffer.
--
<mailto:dietmar.kuehl@claas-solutions.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/05/01 Raw View
"Michel Michaud" <Michel.Michaud@sympatico.ca> writes:
>On page 644 of "The C++ Programming Language, 3rd ed." Dr.Stroustrup
>wrote:
> "Flushing an istream is done using sync()."
>He then explains that it cannot always be done right: I understood
>but it seemed to me it should usually work with cin, so I tried it
>with VC++ and it did indeed work well. However this really means
>nothing of course, and saying that "it cannot be always be done
>right" sounds a bit like "undefined behavior", although it would be
>contrary to saying "Flushing an istream is done using sync()."
>So I checked the standard.
> p.639: basic_istream::sync is described as rdbuf()->pubsync().
>Ok. rdbuf() is a basic_streambuf so:
> p.623: basic_streambuf::pubsync is described as sync(). (!)
>Ok. Then
> p.626: basic_streambuf::sync is described as
> "Synchronizes the controlled sequences with the array. That is,
> if pbase() is non-null the characters between pbase() and pptr()
> are written to the controlled sequence. The pointers may then
> be reset as appropriate."
>Nothing undefined here, from the start to the finish but wait...
>"the characters ... are WRITTEN"? That really looks like a "flush"
>on an OUTPUT stream! So it is really undefined for an istream,
>although the logic of the standard would then be really strange
>(because the conclusion is reached from basic_istream), and
>Dr.Stroustrup would be wrong !?
>I guess there is something missing... Does anyone has any idea?
The actual function of flush and sync on an input stream are
defined by the implementation, or by classes the programmer derives
from the standard iostream classes.
The standard requires nothing beyond "Synchronizes the controlled
sequences with the array," which is not very specific.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]