First of all some code that behaves as expected:

#include <iostream>
#include <sstream>
using namespace std;

int main(int argc, char argv[])
{
  stringstream name;
  name << "abcde";
  name << 123;
  cout << name.str() << endl;
}

Output: abcde123

Now if we change the first two lines of main() to get:

int main(int argc, char argv[])
{
  stringstream name("abcde");
  name << 123;
  cout << name.str() << endl;
}

the output changes to: 123de. So the initialization string gets overwritten
instead of appending the data. Inserting name.seekp(0, ios::end); helps, but
only for compiler version 4.0, not for 4.1 (at least I think so, I couldn't
test this enough ATM).

Now trying to get the right behaviour by using stringstream name("abcde",
ios::app); or stringstream name("abcde", ios::ate); leads to another
interesting output: abcde. The input gets completely ignored.

In my Stroustrup I only found a specification for the behaviour of
ostringstream, not for stringstream but using ostringstream instead of
stringstream doesn't change anything and I don't think stringstream should
behave like this anyway (especially in contrast to the first example).

I hope I didn't waste your time because the bug already got fixed in 4.3 or
something, but I couldn't find anything neither in Bugzilla nor via Google.

Many thanks and kind regards,

Christian

PS: I tested this with g++ 4.0.3 on Ubuntu GNU/Linux, with g++ 4.1 on some SuSE
and with g++ 3.4.3 on SunOS 5.10, all Intel CPUs.


-- 
           Summary: stringstream behaves unexpectedly
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: cmertes at techfak dot uni-bielefeld dot de


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31456

Reply via email to