简体   繁体   English

如何创建一个模板化函数,该函数采用作为 boost::bind 结果的参数?

[英]How can I make a templated function that takes a parameter that is the result of boost::bind?

I have a helper method that takes as input a boost::function<> type object and wraps the function with another functor that handles some other logistics.我有一个辅助方法,它将boost::function<>类型对象作为输入,并用另一个处理其他逻辑的函子包装该函数。

Here is what my signature looks like:这是我的签名的样子:

class Example {
public:
    typedef ... Callback;

    ...

    template<typename T>
    static Callback make_wrapper( const boost::function<void( T )>& );
};

If I try to pass make_wrapper the result of calling boost::bind inline I get compilation errors about types being incompatible (Apple LLVM version 7.3.0)如果我尝试传递 make_wrapper 调用boost::bind内联的结果,我会收到有关类型不兼容的编译错误(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 ) );

this gives:这给出:

error: no matching function for call to 'make_wrapper'
note: candidate template ignored: could not match 'function' against 'bind_t'

I have found 2 ways around this:我找到了两种解决方法:

  1. Temp variable:温度变量:

     boost::function<void( uint32_t )> f = boost::bind( &OtherClass::method, other, _1 ); Example::Callback c = Example::make_wrapper ( f );
  2. Call specific specialization of make_wrapper:调用 make_wrapper 的特定专业化:

     Example::Callback c = Example::make_wrapper<uint32_t> ( boost::bind( &OtherClass::method, other, _1 ) );

I would much prefer it if I could skip the extra hinting and call make_wrapper with the inline call to bind.如果我可以跳过额外的提示并使用内联调用来调用 make_wrapper 以进行绑定,我会更喜欢它。

Is there a way that I can declare the signature of the make_wrapper template to help the compiler figure out the type without needing to use one of workarounds above?有没有一种方法可以声明 make_wrapper 模板的签名以帮助编译器找出类型而无需使用上述解决方法之一?

Whenever you use bind you discard all information about the bound function's parameter types.每当您使用bind您都会丢弃有关绑定函数的参数类型的所有信息。 A function template can't possibly deduce the parameter type T , because the return value of bind is a function object that can be called with any number of parameters of any types.函数模板不可能推导出参数类型T ,因为bind的返回值是一个函数对象,可以使用任意数量的任意类型的参数调用。

You could wrap the bind function into helper function template to deduce the bound member function and especially its result type and parameters (example uses std::bind and std::function but I believe it can be easily transformed to boost ):您可以将bind函数包装到辅助函数模板中以推导出绑定成员函数,尤其是其结果类型和参数(示例使用std::bindstd::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));
}

The code will work as long as foo::bar is not overloaded in which case you can't avoid static_cast .只要foo::bar没有重载,代码就会工作,在这种情况下,您无法避免static_cast

Both std::bind and boost::bind list the return type as unspecified. std::bindboost::bind将返回类型列为未指定。 This means you simply cannot know that, if you want to be at all portable.这意味着如果你想要便携,你根本无法知道这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写以通用迭代器为参数的模板化 function - How can one write a templated function that takes a generic iterator as a parameter 采用模板化长度参数的 Function - Function that takes templated length parameter 如何boost :: bind以通用引用作为参数的模板成员函数 - How to boost::bind a template member function which takes a universal reference as a parameter 如何使用boost :: bind绑定类成员函数? - How can I use boost::bind to bind a class member function? 如何使用以集合为参数的模板化客户端display()函数 - How to use a templated client display() function that takes a set as the parameter 如何制作一个将任意函数指针作为参数的函数? - How can you make a function that takes as a parameter an arbitrary function pointer? 使用boost :: bind结果作为参数 - Using a boost::bind result as a parameter 如何定义一个模板化的 function ,它需要一个 map 和一个 lambda 来按值搜索映射对? - How can I define a templated function which takes a map and a lambda to search map-pair by value? boost :: bind&boost ::函数指针指向重载或模板化的成员函数 - boost::bind & boost::function pointers to overloaded or templated member functions 如何将boost绑定转换为C函数指针? - How can I cast or convert boost bind to C function pointer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM