Home Dashboard Directory Help
Search

Visual C++ ostringstream & ios_base::app broken by ixSci


Status: 

Closed
 as Fixed Help for as Fixed


1
0
Sign in
to vote
Type: Bug
ID: 692248
Opened: 9/30/2011 6:53:42 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

here the code:
#include <iostream>
#include <string>
#include <sstream>

std::string operator+(const std::string& Lhs, int Rhs)
{
    std::ostringstream Stream(Lhs, std::ios_base::out | std::ios_base::app);
    Stream << Rhs;
    return Stream.str();
}

int main()
{
    std::string str("20");
    std::cout << str + 12;
    return 0;
}

And it prints 12. It should print 2012. With the ios_base::ate flag it works fine
Details
Sign in to post a comment.
Posted by Microsoft on 12/16/2011 at 6:47 PM
Hi,

Thanks for reporting this bug. We've fixed it, and the fix will be available in VC11.

VC10 SP1 printed "12". Before our fix, VC11 printed "20". After our fix, VC11 prints "12" again. GCC 4.6.1 prints "2012", but "12" is actually correct. The Standardese for this is:

C++11 27.8.4.1 [ostringstream.cons]/2: "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)) (27.8.2.1)."

C++11 27.8.2.1 [stringbuf.cons]/3: "explicit basic_stringbuf(const basic_string<charT,traits,Allocator>& s, 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() (27.6.3.1), and initializing mode with which. Then calls str(s)."

C++11 27.8.2.3 [stringbuf.members]/2-3: "void str(const basic_string<charT,traits,Allocator>& s);
Effects: Copies the content of s into the basic_stringbuf underlying character sequence and initializes the input and output sequences according to mode.
Postconditions: If mode & ios_base::out is true, pbase() points to the first underlying character and epptr() >= pbase() + s.size() holds; in addition, if mode & ios_base::ate is true, pptr() == pbase() + s.size() holds, otherwise pptr() == pbase() is true. If mode & ios_base::in is true, eback() points to the first underlying character, and both gptr() == eback() and egptr() == eback() + s.size() hold."

This says that the "20" should be overwritten unless ios_base::ate is specified.

If you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by MS-Moderator07 [Feedback Moderator] on 10/2/2011 at 8:16 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by MS-Moderator01 on 9/30/2011 at 7:42 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.