boost::packaged_task生成的future是否抛出错误的异常
Does the future generated from boost::packaged_task throw wrong exception?
我发现boost::packaged_task和std::package d_task之间有明显不同的行为。我测试了boost版本1.55和1.56,c++11编译器是Visual Studio 2013和gcc(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:未知异常
在gcc中(在XCode中)是:catch-std::exception:std::exception
但是,如果我将packaged_task的类型更改为Visual Studio 2013或gcc提供的c++11,那就是:
typedef std::packaged_task<void()> packaged_task;
结果变得不同:捕获my_error
我认为std::packaged_task
工作正常,因为我可以捕捉到真正的类型。我是否滥用boost::packaged_task
?
一个关键区别可能是标准库package_task可能是静态链接的,而boost::thread版本是动态链接的。
跨越动态库边界抛出异常可能并不总是有效的,尤其是当源代码不是用完全相同的编译器选项编译时。
你可以
- 检查库是否使用相同的标志编译
- 使用Boost线程库的静态版本(Boost_Thread_static、Boost_ALL_NO_LIB可能与快速搜索有关),另请参阅http://www.boost.org/boost-build2/doc/html/bbv2/tutorial/linkage.html
相关文章:
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- 检查 n2 是否是 n1 的倍数后结果错误,但根本没有错误
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 我的代码中是否有任何类型的错误,因为它没有给出正确的输出
- C++ LeetCode #377 的 DP 解决方案中,此代码是否有错误?
- GCC 编译器是否应该对涉及 [[fallthrough]] 属性的格式错误的C++代码进行诊断?
- 以下 OpenCV 源代码中是否存在错误?(#ifdef 没有 #else)
- 是否与赢得64位有关?错误:STATUS_ACCESS_VIOLATION
- 对逻辑使用std::out_of_range是否错误
- gcc 在这个概念定义中是否错误地评估了 std::d eclval ?
- C++ - 我是否错误地使用了fin.ignore()?
- 我是否错误地使用Xcode或错误地编写了该程序
- 我是否错误地使用了默认参数
- 我是否错误地使用了 ncurses 库中的 getch() 函数
- C++我是否错误地使用了void函数
- 我是否错误地使用了布尔运算符
- MSVC 是否错误地处理了类范围的静态与整数常量初始值设定项的链接
- 我是否错误地使用了Windows剪贴板?