C++ BOOST\u THROW\u异常导致中止陷阱
我试图使用C++ BOOST\u THROW\u异常导致中止陷阱,c++,exception,boost,C++,Exception,Boost,我试图使用boost::exception并将原型代码压缩为中的示例,但是当使用boost\u THROW\u exception宏运行代码时,我从程序中得到一个中止 #include <iostream> #include <boost/exception/all.hpp> typedef boost::error_info<struct tag_my_info,int> my_info; struct my_error: virtual boost::e
boost::exception
并将原型代码压缩为中的示例,但是当使用boost\u THROW\u 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;
}
如果如评论所说,我交换了代码,一切都很好
$ ./a.out
My info: 42
下面是f()的g++预处理器的输出
我在OSX SL上使用MacPorts提供的工具。我仔细检查了g++搜索路径,发现boost hpp文件只有一个副本,那就是属于前面提到的boost包的文件
我不知道为什么要调用中止陷阱。我承认我对C++很陌生。MP系统中有大量与异常和中止陷阱相关的票据(谷歌上也有大量的例子)
使用XCode附带的g++版本可以解决此问题。此问题是由使用MacPorts版本的g++引起的。MP系统中有大量与异常和中止陷阱相关的票据(谷歌上也有大量的例子)
使用XCode附带的g++版本可以解决这个问题
$ ./a.out
My info: 42
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