c++ - istream 的 operator>> (double& val) 在 libc++ 和 libstdc++ 之间的差异

标签 c++ stringstream istream libstdc++ libc++

随着我最近升级到 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 转换说明符不允许 dX 字符,因此不会累积该字符。它也不会被丢弃(只能丢弃组分隔符)。因此阶段 2 必须在此时结束。

然后在第 3 阶段转换累积的字符。

看起来 libc++ 在第 2 阶段错误地累积了 dX,然后尝试转换它们,但失败了。

关于c++ - istream 的 operator>> (double& val) 在 libc++ 和 libstdc++ 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32205931/

相关文章:

c++ - 智能指针的概念

c++ - 如何配置 Jenkins 以使用 Qt 构建解决方案?

c++ - 从组件构造字符串的最佳方法,iostream 样式

c++ - 在 C++ 中,当 cin 是 bash heredoc 时调用 fork 会导致重复输入片段

c++在类头中用ostream声明一个函数

c++ - 如何使用不同类型的键搜索 std::map

c++ - C++ 中的自动代码生成

c++ - 使用 stringstream 将字符串转换为数字

c++ - C++中的字符串流来解析单词和数字的字符串

c++ - 如何检查 std::cin 是否与终端或管道关联