[英]std::bind function taking std::unique_ptr as argument
請幫我解決我面臨的約束問題......
我有一個類成員方法:
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 );
然后,使用此簽名在我的threadpool add方法中使用std :: function指針:
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但是還沒有工作......
我會很高興為正確的方向提供任何幫助或幫助。 提前致謝! 馬丁
似乎我的線程池的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
的情況。 我不知道如何在這里調用移動構造函數而不是刪除復制一個。 如果有人有想法,請發帖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.