std::istringstream >> to double weird behaviour

↘锁芯ラ 提交于 2019-12-10 14:36:36

问题


The following code prints 0 on mac osx with clang. Everywhere else it prints 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

When I use int it prints 5 as expected..

How does operator>> of std::istringstream work and why this is happening? Is there a way to make it work in a consistent manner? (that is extract 5)


回答1:


The relevant part of the standard is [istream.formatted.arithmetic]. The extractor behavior depends on the locale's num_get<> object.

There is a bug in the libc++ num_get::do_get function for double, described in this bug report. On other platforms, you are likely using libstdc++, which gives the behavior you expect.



来源:https://stackoverflow.com/questions/32167735/stdistringstream-to-double-weird-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!