2017-05-28 127 views
1

請幫我我所面臨的問題,結合...的std :: bind函數採取的std ::的unique_ptr作爲參數

我有一個類成員方法:

std::unique_ptr<TopoDS_Shape> test(std::unique_ptr<TopoDS_Shape> Shape) const 
{ 
    std::unique_ptr<TopoDS_Shape> Result = std::make_unique<TopoDS_Shape>(); 
    return(std::move(Result)); 
} 

我需要構建一個std::function這個指針。爲了做到這一點,我需要使用std::bind轉發此指針作爲test(...)是一個類的成員方法:

std::function<std::unique_ptr<TopoDS_Shape>(std::unique_ptr<TopoDS_Shape>)> tTestFunction; 

tTestFunction = std::bind(&ComponentModelModifier::test, this, std::placeholders::_1); 

然後,的std ::函數指針在我的線程池使用此簽名添加方法:

template<typename FUNCTION, typename... ARGUMENTS> 
auto ThreadPool::add(FUNCTION&& Function, ARGUMENTS&&... Arguments) -> std::future<typename std::result_of<FUNCTION(ARGUMENTS...)>::type> 

召喚:

std::unique_ptr<TopoDS_Shape> tShape = std::make_unique<TopoDS_Shape>(); 

ThreadPool tp; 
std::future<std::unique_ptr<TopoDS_Shape>> ResultFuture = tp.add(tTestFunction, std::move(tShape)); 

但構建總是失敗:

error: no type named ‘type’ in ‘class std::result_of<std::reference_wrapper<std::_Bind<std::function<std::unique_ptr<TopoDS_Shape>(std::unique_ptr<TopoDS_Shape>)>(std::unique_ptr<TopoDS_Shape>)> >()>’ 
     typedef typename result_of<_Callable(_Args...)>::type result_type; 

我知道std :: unique_ptr不是可複製構造的。這就是爲什麼我試圖使用std :: move但沒有任何工作...

我會很高興任何幫助或踢在正確的方向。提前致謝! Martin

+1

您的函數是否確實接受'std :: unique_ptr Shape' _by value_? – Petr

+0

我說是的。該函數應該取代用作參數的唯一指針的所有權。我希望你通過測試()... –

+0

這是你的真實代碼嗎? 'reference_wrapper'從哪裏來的? –

回答

0

看來我的線程池的add()方法沒有成功推導出參數類型,因此報告了上面顯示的錯誤消息。我試圖在我的函數調用中更具體 - 特別是在我試圖指定結果類型的std::move中。另外我需要修改test()方法簽名來改爲使用共享指針(請參閱參數類型)。見下面我的代碼:

std::unique_ptr<TopoDS_Shape> test(const std::shared_ptr<TopoDS_Shape> Shape) const 
{ 
    std::unique_ptr<TopoDS_Shape> Result = std::make_unique<TopoDS_Shape>(); 
    return(std::move(Result)); 
} 

... 

std::future<std::unique_ptr<TopoDS_Shape>> ResultFuture = tp.add(tTestFunction, std::move<std::shared_ptr<TopoDS_Shape>>(tShape)); 

然後我能夠編譯代碼...現在還不清楚爲什麼std::unique_ptr不能作爲test()方法參數類型,但至少我有一個可行的解決方案。 也許它與std :: function的用法有關,它必須具有所有參數copy-constructible而不是std::unique_ptr的情況。我不知道如何在這裏調用移動構造函數而不是刪除副本。 如果有人有想法,請發帖。

相關問題