【发布时间】:2014-03-06 19:16:47
【问题描述】:
这个简单的代码:
#include <iostream>
#include <sstream>
int main()
{
float x = 0.0;
std::stringstream ss("NA");
ss >> x;
std::cout << ( ss.eof() ? "is" : "is not") << " at eof; x is " << x << std::endl;
}
根据我选择的库返回不同的结果(我在 OSX 10.9 上从 xcode 5 运行 clang):
clang++ -std=c++11 -stdlib=libstdc++ -> not at eof
clang++ -stdlib=libstdc++ -> not at eof
/usr/bin/g++-4.2 -> not at eof
clang++ -std=c++11 -stdlib=libc++ -> at eof
clang++ -stdlib=libc+ -> at eof
在我看来,如果我尝试将字母字符读入浮点数,操作应该会失败,但它不应该吃掉无效字符 - 所以我应该得到 fail() 而不是 eof(),所以这个看起来像 libc++ 中的一个错误。
在某处是否有描述行为应该是什么的 c++ 标准?
附言我已经像这样扩展了原始测试程序:
#include <iostream>
#include <sstream>
int main()
{
float x = 0.0;
std::stringstream ss("NA");
ss >> x;
std::cout << "stream " << ( ss.eof() ? "is" : "is not") << " at eof and " << (ss.fail() ? "is" : "is not") << " in fail; x is " << x << std::endl;
if (ss.fail())
{
std::cout << "Clearing fail flag" << std::endl;
ss.clear();
}
char c = 'X';
ss >> c;
std::cout << "Read character: \'" << c << "\'" << std::endl;
}
这就是我所看到的:
使用 libc++:
stream is at eof and is in fail; x is 0
Clearing fail flag
Read character: 'X'
使用 stdlibc++:
stream is not at eof and is in fail; x is 0
Clearing fail flag
Read character: 'N'
p.p.s.如问题中所述,n.m.链接到,如果 stringstream 设置为“MA”而不是“NA”,则不会出现问题。显然,libc++ 开始解析字符串,认为它会得到“NAN”,然后当它没有时,它就会变得很不安。
【问题讨论】:
-
这与here描述的问题相同。
-
nm,我看过那个问题,它看起来非常相似,但由于我的问题是 eof() 而另一个问题抱怨 fail() 我不确定根本问题是否是相同的。查看为其他问题提交的错误,目前尚不清楚修复该错误是否也能解决我的问题......但你知道图书馆的内部运作比我好 100 倍!
-
这是同一个问题,即
NA是否被失败的输入操作消耗。
标签: c++ stringstream istream libstdc++ libc++