【发布时间】:2016-10-14 21:22:58
【问题描述】:
我有一个辅助方法,它将boost::function<> 类型的对象作为输入,并用另一个处理其他物流的函子包装该函数。
这是我的签名的样子:
class Example {
public:
typedef ... Callback;
...
template<typename T>
static Callback make_wrapper( const boost::function<void( T )>& );
};
如果我尝试将调用 boost::bind 的结果传递给 make_wrapper,我会收到关于类型不兼容的编译错误(Apple LLVM 版本 7.3.0)
class OtherClass {
public:
void method ( uint32_t );
};
OtherClass* other;
Example::Callback c = Example::make_wrapper ( boost::bind( &OtherClass::method, other, _1 ) );
这给出了:
error: no matching function for call to 'make_wrapper'
note: candidate template ignored: could not match 'function' against 'bind_t'
我找到了两种解决方法:
-
临时变量:
boost::function<void( uint32_t )> f = boost::bind( &OtherClass::method, other, _1 ); Example::Callback c = Example::make_wrapper ( f ); -
调用 make_wrapper 的特定特化:
Example::Callback c = Example::make_wrapper<uint32_t> ( boost::bind( &OtherClass::method, other, _1 ) );
如果我可以跳过额外的提示并使用内联调用调用 make_wrapper 来绑定,我会更喜欢它。
有没有一种方法可以声明 make_wrapper 模板的签名以帮助编译器确定类型,而无需使用上述解决方法之一?
【问题讨论】:
-
使用
auto或decltype
标签: c++ boost boost-bind