为什么在FreeBSD 9上无法捕获std :: bad_cast? [英] Why does catching std::bad_cast not work on FreeBSD 9?
问题描述
考虑以下代码(badcast.cpp):
Consider this code (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("%d\n", b.val);
} catch (const std::bad_cast &) {
printf("bad cast\n");
}
}
int main() {
foo f;
cast_test(f);
return 0;
}
FreeBSD 9.1:
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:
Debian Linux 6:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
bad cast
OS X 10.8:
OS X 10.8:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
bad cast
为什么在FreeBSD上捕获bad_cast不起作用?
Why does catching bad_cast not work on FreeBSD?
推荐答案
作为一个疯狂的猜测,在FreeBSD中,您可能会使用LLVM的新 libc++
,而不是旧的GNU libstdc++
. FreeBSD已经致力于切换到
As a wild guess, there’s a chance in FreeBSD you might be using LLVM’s new libc++
, instead of the old GNU libstdc++
. FreeBSD has been working towards switching over to the LLVM toolchain, away from GNU GPL licensed software.
苹果也在朝着这个方向发展,过去我遇到了使用libc++
没有的(特别是使用Boost)使用libc++
为Mac开发的问题.
Apple’s moving that way too, and in the past I’ve ran into issues developing for Mac using libc++
that libstdc++
didn’t have (especially with Boost).
您可以使用ldd
确认要链接的库:
You can use ldd
to confirm what libraries you’re linking against:
ldd ./badcast
如果它链接到libc++
,则可能要使用 LLVM项目提交错误和测试用例.
If it is linking against libc++
, you might want to file the bug and test case with the LLVM project.
这篇关于为什么在FreeBSD 9上无法捕获std :: bad_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!