istream运算符>>之间的差异。 (double& val)在libc ++和libstdc ++之间 [英] Discrepancy between istream's operator>> (double& val) between libc++ and libstdc++

查看:108
本文介绍了istream运算符>>之间的差异。 (double& val)在libc ++和libstdc ++之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我最近升级到Mac OS X 10.9,默认标准C ++库已从libstdc ++更改为libc ++。从那时起,我观察到在下面的代码示例中记录的stringstream运算符>>(double)的意外行为。



总而言之,当double值后跟字母时,libc ++似乎在从stringstreams中提取双精度值有问题。



我已经检查标准(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()<< '转换为<< d<< endl;
else
cout<< '< ss.str()<< '无法转换为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'转换为-4.9 
' X'转换为-4.9
'-4.9_'转换为-4.9
'-4.9d'无法转换为double
'-4.9X'无法转换为double

使用 c ++编译代码--stdlib = libstdc ++ streamtest.cxx 给予

 ' -  4.9'转换为-4.9 
'-4.9 X'转换为-4.9
'-4.9_'转换为-4.9
'-4.9d'转换为-4.9
'-4.9X'转换为-4.9

编译器版本为

  $ c ++ --version 
Apple LLVM版本5.0(clang-500.2.79)(基于LLVM 3.3svn)
目标:x86_64-apple-darwin13.0.0
线程模型:posix


解决方案

看起来libstdc ++是正确的,libc ++是错误的,根据22.4.2.1.2



在阶段2,


[字符 - nm]不被丢弃,则进行检查以确定是否允许c作为阶段1返回的转换说明符的输入字段的下一个字符[%g在这种情况下为nm。


由于%g 转换说明符允许 d X 个字符,字符不会累积。它也不会被丢弃(只能删除组分隔符)。因此,阶段2必须在此时结束。



然后在阶段3,累积的字符将被转换。



像libc ++在阶段2错误地累积 d X ,然后尝试转换它们,并且失败。


With my recent upgrade to Mac OS X 10.9 the default standard C++ library changed from libstdc++ to libc++. Since then I observe unexpected behaviour of the stringstream operator>>(double) documented in the code example below.

In summary the libc++ seems to have problems with extracting double values from stringstreams when the double value is followed by a letter.

I already checked the standard (2003) but I can't find any specific information if extraction should work in this case or not.

So I would be grateful for any input whether this is a bug in libc++ or 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");
}

Compiling the code with c++ --stdlib=libc++ streamtest.cxx gives

'-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

Compiling the code with c++ --stdlib=libstdc++ streamtest.cxx gives

'-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

Compiler version is

$ 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

解决方案

It looks like that libstdc++ is right and libc++ is wrong, according to the 22.4.2.1.2 of the (2011) standard.

At stage 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.

Since %g conversion specifier does not admit d or X characters, the character is not accumulated. It is not discarded either (only group separator characters can be discarded). Therefore Stage 2 must end at this point.

Then at stage 3 accumulated characters are converted.

It looks like libc++ erroneously accumulates d and X at stage 2, then attempts to convert them, and this fails.

这篇关于istream运算符&gt;&gt;之间的差异。 (double&amp; val)在libc ++和libstdc ++之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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