我有一个助手方法,它以一个boost::function<>类型的对象作为输入,并将函数封装到另一个处理其他物流的函子中。
以下是我的签名:
class Example {
public:
typedef ... Callback;
...
template<typename T>
static Callback make_wrapper( const boost::function<void( T )>& );
};如果我试图传递make_wrapper (内联调用boost::bind的结果),就会得到类型不兼容的编译错误(AppleLLVM7.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'我发现了两种解决方法:
我更希望可以跳过额外的提示,用内联调用来绑定调用make_wrapper。
我是否可以声明make_wrapper模板的签名,以帮助编译器在不需要使用上述解决方案的情况下确定类型?
发布于 2016-10-15 11:46:19
无论何时使用bind,都会丢弃有关绑定函数的参数类型的所有信息。函数模板不可能推断参数类型T,因为bind的返回值是一个函数对象,可以用任意数量的任何类型的参数调用它。
您可以将bind函数封装到助手函数模板中,以推断绑定的成员函数,特别是其结果类型和参数(示例使用std::bind和std::function,但我相信可以轻松地将其转换为boost):
#include <iostream>
#include <string>
#include <functional>
struct foo {
void bar(int a, std::string s) {
std::cout << a << " " << s << std::endl;
}
};
template<typename T1, typename T2>
void make_wrapper(const std::function<void( T1, T2 )>&) {
}
template <class Foo, class Res, class... Args, class... Placeholders>
std::function<Res(Args...)> my_bind(Res (Foo::*bar)(Args...), Foo& f, Placeholders... ps) {
return std::bind(bar, f, ps...);
}
int main() {
foo f;
make_wrapper(my_bind(&foo::bar, f, std::placeholders::_1, std::placeholders::_2));
}只要foo::bar没有重载,代码就可以工作,在这种情况下,您无法避免static_cast。
发布于 2016-10-14 21:29:46
std::bind和boost::bind都将返回类型列为未指定的类型。这意味着你根本不知道这一点,如果你想要在任何便携式。
https://stackoverflow.com/questions/40052202
复制相似问题