With my recent upgrade to Mac OS X 10.9 the default standard C++ library changed from libstdc++ to libc++. Since then I observe unexpected behaviour of the stringstream operator>>(double) documented in the code example below.
In summary the libc++ seems to have problems with extracting double values from stringstreams when the double value is followed by a letter.
I already checked the standard (2003) but I can't find any specific information if extraction should work in this case or not.
So I would be grateful for any input whether this is a bug in libc++ or libstdc++.
#include <sstream>
#include <iostream>
using namespace std;
void extract_double(const string & s)
{
stringstream ss;
double d;
ss << s;
ss >> d;
if(!ss.fail())
cout << "'" << ss.str() << "' converted to " << d << endl;
else
cout << "'" << ss.str() << "' failed to convert to double" << endl;
}
int main()
{
extract_double("-4.9");
extract_double("-4.9 X");
extract_double("-4.9_");
extract_double("-4.9d");
extract_double("-4.9X");
}
Compiling the code with c++ --stdlib=libc++ streamtest.cxx
gives
'-4.9' converted to -4.9
'-4.9 X' converted to -4.9
'-4.9_' converted to -4.9
'-4.9d' failed to convert to double
'-4.9X' failed to convert to double
Compiling the code with c++ --stdlib=libstdc++ streamtest.cxx
gives
'-4.9' converted to -4.9
'-4.9 X' converted to -4.9
'-4.9_' converted to -4.9
'-4.9d' converted to -4.9
'-4.9X' converted to -4.9
Compiler version is
$ c++ --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
See Question&Answers more detail:os