簡體   English   中英

為什么捕獲std :: bad_cast不適用於FreeBSD 9?

[英]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("%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:

$ 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許可軟件。

蘋果也是這樣移動的,過去我遇到了使用libstdc++沒有的libc++為Mac開發的問題(尤其是Boost)。

您可以使用ldd確認要鏈接的庫:

ldd ./badcast

如果它是與libc++鏈接,您可能希望使用LLVM項目提交錯誤和測試用例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM