如何获取promise :: set_exception(x)的参数?

tow*_*owi 11 c++ multithreading exception promise c++11

我在几个地方发现了应该如何使用诺言copy_exception,但我在目前的FDIS中找不到它.set_exception()自那些博客以来,有没有其他方法可以使用?

例如这里

void asyncFun(promise<int> intPromise)
{
    int result;
    try {
        // calculate the result
        intPromise.set_value(result);
    } catch (MyException e) {
        intPromise.set_exception(std::copy_exception(e));  // <- copy
    }
}
Run Code Online (Sandbox Code Playgroud)

我觉得std::current_exception() 在这里.

catch(...)
{
    p.set_exception(std::current_exception());
}
Run Code Online (Sandbox Code Playgroud)

因此我的问题:

  • 我是否应该总是使用current_exception(),即使我没有抓到" ..."?
  • 或者是否有新的不同名称copy_exception

How*_*ant 15

有一个不同的名称copy_exception. copy_exception在标准化过程的后期重新命名,而不是混淆实际做了什么:

template<class E>
   exception_ptr make_exception_ptr(E e) noexcept;
Run Code Online (Sandbox Code Playgroud)

效果:创建一个exception_ptr引用副本的对象 e,......

使用任何一个make_exception_ptrcurrent_exception很好,取决于您尝试设置的异常.