Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/RTL/C++/STLPort    [ Add a report in this area ]  
Report #:  3470   Status: Closed
basic_istream::operator>> returns an undefined value from an empty stream
Project:  C++Builder Build #:  6.0.10.165
Version:    6.0 Submitted By:   Jussi Sirpoma
Report Type:  Minor failure / Design problem Date Reported:  2/2/2003 7:47:48 PM
Severity:    Infrequently encountered problem Last Updated: 2/8/2005 1:15:41 PM
Platform:    All versions Internal Tracking #:  
Resolution: Won't Do (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
The basic_istream::operator>>(int&) and basic_istream::operator>>(short&) return an undefined value if the stream is or goes to error state.

Other >> operators do not modify the value given as parameter.

From stl\_istream.h:

  _Self& operator>> (int& __val) {
    long __lval;
    unsigned int __uval;
    _M_get_num(*this, __lval);
    __val = __lval;
    __uval = __lval;
    // check if we lose digits
    //    if ((__val != __lval) && ((unsigned int)__val != __lval))
    if ((__val != __lval) && ((long)__uval != __lval))
      this->setstate(ios_base::failbit);
    return *this;
  }

To make this operator behave the same way as the other >> operators do, the __lval could be initialized with __val.

This problem exist only when using STLPort.
Steps to Reproduce:
Run this program and check the value in tmp after reading it from the stream.

Expected: tmp has 0 in it
Actual Result: tmp has a random value

tmp2 has the expected 0.

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <sstream>
#include <string>

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
     std::string str;
     std::stringstream is;

     int tmp = 0;
     is >> tmp;

     unsigned int tmp2 = 0;
     is >> tmp2;

     return 0;
}
//---------------------------------------------------------------------------

Workarounds
None
Attachment
None
Comments

None

Server Response from: ETNACODE01