boost::promise::set_exception() 编译错误

boost::promise::set_exception() compilation error

我在尝试编译以下代码时遇到错误:

#include <exception>
#include <boost/thread.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
    boost::promise<int> pr;

    pr.set_exception(std::copy_exception(std::runtime_error("test")));

    std::cout << "done" << std::endl;
}

error C2668: 'boost::copy_exception' : ambiguous call to overloaded function d:\projects\boost\boost_1_55_0\boost\thread\future.hpp 2092

我正在使用 VS2010 和 Boost 1.55

供参考:`boost::promise::set_exception'的实现如下:

void set_exception(boost::exception_ptr p)
{
    // exception is stored here
}

template <typename E> void set_exception(E ex)
{
    set_exception(copy_exception(ex));  // <- this is the line 2092
}

所以,有一个模板版本,称为非模板版本。 我假设在我的情况下模板版本失败。

使用以下代码问题消失:

pr.set_exception(boost::copy_exception(std::runtime_error("test")));

即使用 boost::copy_exception() 而不是 std::copy_exception()
谁能建议使用 std::copy_exception 编译代码的选项?

tldr;你应该使用 boost::copy_exception.


boost::copy_exception returns a boost::exception_ptr,所以当我们调用 set_exception() 时,set_exception(exception_ptr ) 重载是首选,因为它是一个非模板。这个电话做对了。

std::copy_exception(现在叫std::make_exception_ptr)returns一个std::exception_ptr。这与 boost::exception_ptr 不是同一类型,因此函数模板 set_exception() 是首选。实例化函数模板会导致对命名空间 boost 内的 copy_exception() 不合格 调用。结果是:

namespace boost {
    template <class T> exception_ptr copy_exception(T const&);
}

因为这只是一个函数,所以我们随后对参数的关联名称空间进行参数相关的查找。 std::exception_ptr 的关联命名空间是 std,因此我们最终还发现:

namespace std {
    template <class E> exception_ptr copy_exception(E );
}

这些函数模板都不比另一个好,因此调用不明确。

由于使用 std::copy_exception 没有任何优势,而且该函数甚至不存在于标准中,因此使用 boost::copy_exception。它做你想做的。


或者,您可以只使用 std::promise,它的 set_exception() 只有一个需要 std::exception_ptr 的重载。