【问题标题】:std::istringstream >> to double weird behaviourstd::istringstream >> 将奇怪​​的行为加倍
【发布时间】:2015-11-17 00:24:16
【问题描述】:

以下代码在 mac osx 上使用 clang 打印 0。它在其他任何地方打印 5 (clang, gcc)

#include <iostream>
#include <sstream>

int main() {
    std::istringstream iss("5C3");

    double n;
    iss >> n;

    std::cout << n << std::endl;

    return 0;
}

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

当我使用 int 时,它会按预期打印 5..

operator>> of std::istringstream 是如何工作的,为什么会这样?有没有办法让它以一致的方式工作? (即摘录5)

【问题讨论】:

    标签: c++ macos c++11 clang clang++


    【解决方案1】:

    标准的相关部分是[istream.formatted.arithmetic]。提取器行为取决于语言环境的 num_get&lt;&gt; 对象。

    double 的 libc++ num_get::do_get 函数中有一个错误,描述为 in this bug report。在其他平台上,您可能正在使用 libstdc++,它提供了您所期望的行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 2020-06-11
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多