【发布时间】:2017-05-28 09:36:18
【问题描述】:
请帮我解决我面临的绑定问题...
我有一个类成员方法:
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::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 但没有任何效果......
如果有任何帮助或朝着正确的方向发展,我会很高兴。提前致谢!马丁
【问题讨论】:
-
您的函数是否确实接受
std::unique_ptr<TopoDS_Shape> Shape按值? -
我说是的。该函数应该超越用作参数的唯一指针的所有权。我希望你 ment test()...
-
这是你的真实代码吗?
reference_wrapper是从哪里来的? -
是的。我做了一点跟踪,似乎
std::reference_wrapper来自std::result_of,正如auto ThreadPool::add( FUNCTION&& Function, ARGUMENTS&&... Arguments ) -> std::future<typename std::result_of<FUNCTION(ARGUMENTS...)>::type>中使用的那样 -
你能给我们看一个实际的minimal reproducible example而不是小sn-ps吗?
标签: c++ binding c++14 move unique-ptr