C++ std::ifstream中出现意外异常
在尝试I/O时,我得到一个不应引发异常的异常:C++ std::ifstream中出现意外异常,c++,iostream,C++,Iostream,在尝试I/O时,我得到一个不应引发异常的异常: #include <iostream> #include <fstream> int main() { std::ifstream f("/tmp"); std::cout << "Exception Flags: " << f.exceptions() << std::endl; if(f >> std::ws) std::cout <<
#include <iostream>
#include <fstream>
int main()
{
std::ifstream f("/tmp");
std::cout << "Exception Flags: " << f.exceptions() << std::endl;
if(f >> std::ws) std::cout << "This will not succeed" << std::endl;
else std::cout << "Ok - it fails" << std::endl;
return 0;
}
g++(Ubuntu/Linaro 4.7.2-2ubuntu1)4.7.2
编辑
测试应无一例外地失败:
从27.5.5.4基本ios标志函数
无效清除(iostate状态=goodbit)
4后置条件:如果rdbuf()=0然后state==rdstate();否则rdstate()=(state | ios_base::badbit)
5效果:如果((状态|(rdbuf()?好比特:坏比特))&
异常())==0,返回。否则,函数将抛出
类basic_ios::failure(27.5.3.1.1)的对象失败,由
实现定义的参数值
无效设置状态(iostate状态)
6效果:调用清除(rdstate()|
状态)(可能引发基本IO::故障(27.5.3.1.1))
这是一个bug,并且与libc++兼容的v3.4版本;GCC4.8的libstdc++ 从libstdc++源代码和异常的文本来看,我认为这并不是捕获从调用堆栈中抛出的异常,并在潜在地重新调用它们之前应用“是异常标志在起作用吗?”逻辑
去年它已经被提升为。旁注:使用clang的结果是:使用'f>>std::ws'时“这不会成功”,使用'f>>x'时“Ok-它失败”,其中x是一个整数。@Dieter:Yep;见第一段中的链接
Exception Flags: 0
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_filebuf::underflow error reading the file
Aborted