std :: istringstream>>加倍怪异的行为 [英] std::istringstream >> to double weird behaviour

查看:94
本文介绍了std :: istringstream>>加倍怪异的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在带有c声的mac osx上打印 0 .它会在其他任何地方打印 5 (( c gcc )

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

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

When I use int it prints 5 as expected..

std :: istringstream的 operator >>的工作原理以及为什么这样做正在发生?有没有办法使它以一致的方式工作? (即摘录5)

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)

推荐答案

标准的相关部分是[istream.formatted.arithmetic].提取程序的行为取决于语言环境的num_get<>对象.

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

double的libc ++ num_get::do_get函数中存在错误,在中进行了描述.此错误报告.在其他平台上,您可能会使用libstdc ++,它可以提供您期望的行为.

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.

这篇关于std :: istringstream&gt;&gt;加倍怪异的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆