随着我最近升级到 Mac OS X 10.9,默认的标准 C++ 库从 libstdc++ 更改为 libc++。从那时起,我观察到下面代码示例中记录的 stringstream operator>>(double) 的意外行为。
总而言之,当 double 值后跟一个字母时,libc++ 似乎无法从字符串流中提取 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
最佳答案
根据(2011)标准的22.4.2.1.2,看来libstdc++是对的,libc++是错的。
在第 2 阶段,
If it [the character - n.m.] is not discarded, then a check is made to determine if c is allowed as the next character of an input field of the conversion specifier returned by Stage 1 ["%g" in this case - n.m.] . If so, it is accumulated.
由于 %g
转换说明符不允许 d
或 X
字符,因此不会累积该字符。它也不会被丢弃(只能丢弃组分隔符)。因此阶段 2 必须在此时结束。
然后在第 3 阶段转换累积的字符。
看起来 libc++ 在第 2 阶段错误地累积了 d
和 X
,然后尝试转换它们,但失败了。
关于c++ - istream 的 operator>> (double& val) 在 libc++ 和 libstdc++ 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32205931/