【发布时间】:2014-11-05 06:44:45
【问题描述】:
我发现 boost::packaged_task 和 std::packaged_task 之间存在明显不同的行为。我测试了boost版本1.55和1.56,c++11编译器是Visual Studio 2013和gcc(in XCode)。
即从packaged_task::get_future()生成的future调用get()会发出不同的异常。
这是我的简单代码:
#include <boost/thread/future.hpp>
#include <future>
#include <iostream>
struct my_error {};
void throw_something()
{
throw my_error();
}
int main()
{
typedef boost::packaged_task<void> packaged_task;
packaged_task task(throw_something);
auto fu = task.get_future();
task();
try {
fu.get();
std::cout << "no exception" << std::endl;
}
catch (const my_error&) {
std::cout << "catch my_error" << std::endl;
} catch (const std::exception &e) {
std::cout << "catch std::exception: " << e.what() << std::endl;
} catch (...) {
std::cout << "catch unknown error" << std::endl;
}
std::system("pause");
return 0;
}
在 Visual Studio 2013 中,结果是:catch std::exception: Unknown exception
在 gcc(在 XCode 中)是:catch std::exception: std::exception
但是如果我把packaged_task的类型改成Visual Studio 2013或者gcc提供的c++11,那就是:
typedef std::packaged_task<void()> packaged_task;
结果变差:catch my_error
我认为std::packaged_task 工作正常,因为我可以捕捉到真正的类型。我会误用boost::packaged_task吗?
【问题讨论】: