【发布时间】:2016-08-29 22:28:07
【问题描述】:
上下文
我有这个简单的代码:
#include <iostream>
#include <fstream>
#include <system_error>
int main(int argc, char *argv[]) {
std::ifstream file;
file.exceptions(std::ios::failbit | std::ios::badbit);
try {
file.open("a_file_does_not_exist.txt", std::ios::in);
file.close();
} catch(const std::ios_base::failure& err) {
std::cerr << err.code() << std::endl;
return -1;
}
return 0;
}
只是为了完成,这是编译命令:
g++ -std=c++11 -g // ...
编译器版本为g++ (GCC) 6.1.1。
平台:arch-linux 4.7.2-1.
问题
你可以想象,文件不存在,所以file.open(...)方法会抛出异常。问题是当我运行代码时未处理异常,并调用了std::terminate。
奇怪的是输出:
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear
Annullato (core dump creato)
如您所见,投掷类是 std::ios_base::failure,但我的发现是正确的。
我的问题是:我错过了什么?
【问题讨论】:
-
在 g++ 5.4.0 中按预期工作
-
失败on Clang 3.8.0。绝对是一个错误。
-
@Cheersandhth.-Alf 但是如果你手动扔一个,它是
NSt8ios_base7failureB5cxx11E。可能代码库中有两种定义,一种用于C++03,一种用于C++11,03在open中隐藏了11? -
是的,
ios_base::failure在 C++11 中进行了 ABI 更改。 libstdc++ 在这里试图管理转换有点混乱。 gcc.gnu.org/bugzilla/show_bug.cgi?id=66145
标签: c++ c++11 exception-handling system-error