2016-07-28 59 views
1

我得到試圖編譯下面的代碼時出錯:的boost ::承諾:: set_exception()編譯錯誤

#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; 
} 

錯誤C2668: '提高:: copy_exception':曖昧通話重載函數d:\項目\提升\ boost_1_55_0 \提升\螺紋\ future.hpp 2092

我使用VS2010和Boost 1.55

對於參考:`的boost ::承諾的實現: :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編譯代碼的選項嗎?

+0

我不知道這是否相關:http://stackoverflow.com/questions/37831458/where-is-stdcopy-exception-defined –

回答

1

tldr;您應該使用boost::copy_exception


boost::copy_exception返回boost::exception_ptr,所以,當我們稱set_exception(),所述過載set_exception(exception_ptr)優選爲一個非模板。這個呼叫做正確的事情。

std::copy_exception(現在稱爲std::make_exception_ptr)返回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

+0

感謝您的詳細解釋模糊呼叫的原因 –