Topic: correct state of stringstream after construction".
Author: Matthias Mueller <matthias@ica1.uni-stuttgart.de>
Date: 1998/05/07 Raw View
Hi,
I have problems with one of the examples in Stroustrup
Book "The C++ programming language 3rd edition":
What is the correct output of the following program?
#include <sstream>
using namespace std;
void A(void){
ostringstream ost;
ost << "one ";
ost << "two";
cout << ost.str() << "\n";
}
void B(void){
ostringstream ost("one ");
ost << "two";
cout << ost.str() << "\n";
}
int main(void){
A();
B();
return 0;
}
According to Bjarne Stroustrup A() and B() should produce the same output.
(C++ Programming Language (3rd eddition), Section 21.5.3 example about
formated message strings).
We have two compilers producing
one two
two
as output. One vendor claims that this is according to the standard.
So lets check what is written there:
In [lib.iostream.forward]:
typedef basic_ostringstream<char> ostringstream;
in [lib.ostringstream.cons]:
explicit basic_ostringstream(
const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::out);
Effects:
Constructs an object of class basic_ostringstream<charT,traits>,
initializing the base class with basic_ostream(&sb) and initializing
sb with basic_stringbuf<charT,traits,Allocator>(str, which |
ios_base::out)) (_lib.stringbuf.cons_).
[lib.stringbuf.cons]:
explicit basic_stringbuf(const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::in | ios_base::out);
Effects:
Constructs an object of class basic_stringbuf, initializing the base
class with basic_streambuf() (_lib.streambuf.cons_), and initializ-
ing mode with which. Then copies the content of str into the
basic_stringbuf underlying character sequence and initializes the
input and output sequences according to which. If which &
ios_base::out is true, initializes the output sequence with the
underlying sequence. If which & ios_base::in is true, initializes
the input sequence with the underlying sequence.
Postconditions:
str() == str.
So far so good, but what is the position of the ouptut sequence after
all this? According to Stroustrup the put pointer of the underlying
streambuf is at the end of the buffer our vendor believes it has to be
at the beginning.
My personal opinion is that A() and B() should produce the same output,
but would prefer to hear the opinion of other people before argueing
with the vendor again.
Matthias
---
[ 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 ]