Mac OS X 10.9 への最近のアップグレードで、デフォルトの標準 C++ ライブラリが libstdc++ から libc++ に変更されました。それ以来、以下のコード例に記載されている stringstream operator>>(double) の予期しない動作を観察しました。
要約すると、libc++ は、double 値の後に文字が続く場合、stringstreams から double 値を抽出する際に問題があるようです。
すでに標準 (2003) を確認しましたが、この場合に抽出が機能するかどうかについての具体的な情報は見つかりません。
したがって、これが libc++ または 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");
}
c++ --stdlib=libc++ streamtest.cxx
を使用してコードをコンパイルすると、
'-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
c++ --stdlib=libstdc++ streamtest.cxx
を使用してコードをコンパイルすると、
'-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
コンパイラのバージョンは
$ 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