【发布时间】:2011-11-24 12:13:51
【问题描述】:
我正在尝试使用 boost::exception 并将我的原型代码压缩为 Boost exception tutorial 中的示例,但是当使用 BOOST_THROW_EXCEPTION 宏运行代码时,我会从程序中止。
#include <iostream>
#include <boost/exception/all.hpp>
typedef boost::error_info<struct tag_my_info,int> my_info;
struct my_error: virtual boost::exception, virtual std::exception { };
void f() {
BOOST_THROW_EXCEPTION(my_error() << my_info(42));
// Uncomment the below (and comment the above) for the program to work
//throw my_error() << my_info(42);
}
int main(int argc, char** argv) {
try {
f();
}
catch(my_error& x) {
if(int const* mi = boost::get_error_info<my_info>(x)) {
std::cout << "My info: " << *mi << std::endl;
}
}
return 0;
}
使用BOOST_THROW_EXCEPTION 宏运行代码:
$ ./a.out
Abort trap
如果如评论所说,我交换代码,一切都很好
$ ./a.out
My info: 42
下面是 f() 的 g++ 预处理器的输出
void f() {
::boost::exception_detail::throw_exception_(my_error() << my_info(42),__PRETTY_FUNCTION__,"main.cpp",14);
}
软件版本是:
$ g++ -v
Using built-in specs.
Target: x86_64-apple-darwin10
Thread model: posix
gcc version 4.4.6 (GCC)
$ port list boost
boost @1.47.0 devel/boost
我在 OSX SL 上使用 MacPorts 提供的工具。我已经仔细检查了 g++ 搜索路径,并且只有一份 boost hpp 文件的副本,并且这些文件属于上述 boost 包。
我不知道为什么要调用中止陷阱。我承认我对 C++ 很陌生。
【问题讨论】: