std :: ifstream中的意外异常

6 c++ iostream

试验I/OI得到一个异常,不应该抛出任何异常:

#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;
}
Run Code Online (Sandbox Code Playgroud)

但输出是:

Exception Flags: 0
terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_filebuf::underflow error reading the file
Aborted
Run Code Online (Sandbox Code Playgroud)

g ++(Ubuntu/Linaro 4.7.2-2ubuntu1)4.7.2

编辑

测试应该毫无例外地失败:

从27.5.5.4 basic_ios标志函数

void clear(iostate state = goodbit);

4后置条件:如果rdbuf()!= 0则state == rdstate(); 否则rdstate()==(state | ios_base :: badbit).

5效果:如果((状态|(rdbuf()?goodbit:badbit))&exceptions())== 0,则返回.否则,该函数抛出一个对象失败的类basic_ios :: failure(27.5.3.1.1),由实现定义的参数值构造.

void setstate(iostate state);

6效果:调用clear(rdstate()| state)(可能会抛出basic_ios :: failure(27.5.3.1.1)).

Lig*_*ica 7

这是一个bug,而使用libc ++的clang v3.4 没有表现出这种行为 ; GCC 4.8的libstdc ++ 仍然可以.

从libstdc ++源代码和异常文本中,我要说的basic_ios::clear是没有捕获从调用堆栈中抛出的异常并应用"在播放中是异常标志吗?" 在重新抛出它们之前的逻辑.

它已经在去年被提出为bug 53984.