为什么在FreeBSD 9上不能捕获std::bad_cast ?
Why does catching std::bad_cast not work on FreeBSD 9?
考虑以下代码(badcast.cpp):
#include <exception>
#include <typeinfo>
#include <stdio.h>
class foo {
public:
virtual ~foo() {}
};
class bar: public foo {
public:
int val;
bar(): val(123) {}
};
static void
cast_test(const foo &f) {
try {
const bar &b = dynamic_cast<const bar &>(f);
printf("%dn", b.val);
} catch (const std::bad_cast &) {
printf("bad castn");
}
}
int main() {
foo f;
cast_test(f);
return 0;
}
FreeBSD 9.1:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Abort trap (core dumped)
$ g++ badcast.cpp -o badcast -frtti -fexceptions -Wall && ./badcast
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Abort trap (core dumped)
$ gcc -v
Using built-in specs.
Target: amd64-undermydesk-freebsd
Configured with: FreeBSD/amd64 system compiler
Thread model: posix
gcc version 4.2.1 20070831 patched [FreeBSD]
$ uname -a
FreeBSD freebsd9 9.1-RELEASE FreeBSD 9.1-RELEASE #0 r243825: Tue Dec 4 09:23:10 UTC 2012 root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC amd64
Debian Linux 6:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
bad cast
OS X 10.8:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
bad cast
为什么捕获bad_cast不能在FreeBSD上工作?
作为一个大胆的猜测,在FreeBSD中,您可能会使用LLVM的新libc++
,而不是旧的GNU libstdc++
。FreeBSD一直致力于切换到LLVM工具链,远离GNU GPL许可软件。
苹果也在朝着这个方向发展,过去我在Mac上使用libc++
开发时遇到了libstdc++
没有的问题(特别是使用Boost)。
你可以使用ldd
来确认你要链接的库:
ldd ./badcast
如果它链接到libc++
,您可能想要将错误和测试用例提交到LLVM项目中。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- "std::vector"在调整大小时引发"bad allocation"异常
- 成员函数 bad() 的 std::ftsream 用于检查
- 正在使用std::string而不是char*bad
- 在抛出 'std::bad _alloc' 的实例后调用终止 what(): std::bad_alloc 在 c++ 中
- What is Scala for: getline(), std::cin.eof(), std::cin.bad()