简体   繁体   English

将C ++模板与std :: function和std :: bind一起使用

[英]Using C++ template with std::function and std::bind

I am trying to create a template class which would in turn generate a wrapper over a function. 我正在尝试创建一个模板类,该模板类又将在函数上生成包装器。 The class will then return the wrapper as result. 然后,该类将返回包装器作为结果。 I would like to use template to have general class that will work with any function with different signatures, such as: 我想使用模板来具有通用类,该通用类可与具有不同签名的任何函数一起使用,例如:

  1. std::function<void()>task = std::bind(fun1, param1, param2);
  2. std::function<int(int, int)>task = std::bind(fun2, param1, param2);

I would like to have something like this: 我想要这样的东西:

template <typename T1, typename T2>
class A {
  A (string param1, string param2) {
    // The created wrapper here i.e. 'task' will be returned by the class.
    function<T1>task = bind(T2, param1, param2);
  }

  // Return the created wrapper in the constructor.
  function<T1> returnWrapper() {
    return task;
  }
};

The code above is mostly a pseudo code since it cannot be compiled, but gives an idea about what I am looking for. 上面的代码大部分是伪代码,因为它无法编译,但可以让我对所要查找的内容有所了解。 Is there any solution for this? 有什么解决办法吗? I think there should be more than simply use a template for a function's signature. 我认为应该不仅仅是为函数签名使用模板。 Any help would be highly appreciated. 任何帮助将不胜感激。 I also would like to be able to pass arbitrary number of parameters to 'bind' if possible. 我还希望能够在可能的情况下将任意数量的参数传递给“绑定”。

I think I solved the problem! 我想我解决了问题! I had to define a class which takes two type names inside a template and pass one of them to std::function as function signature after currying and use the second one in the constructor to define the curried function (result function after wrapping) in std::bind. 我必须定义一个类,该类在模板内使用两个类型名称,并将其中一个类型名称传递给std :: function,作为函数签名,然后在构造函数中使用第二个名称在std中定义curried函数(包装后的结果函数) :: bind。 Then everything worked fine! 然后一切正常! There might be some better solution, but this was the best and more or less clear solution I got. 可能有一些更好的解决方案,但这是我得到的最好的或差不多的清晰解决方案。 Here is the got snippet of the solution I found! 这是我找到的解决方案的摘要! Hope it helps the other with the same issue: 希望它对其他问题有帮助:

#include <iostream>
#include <functional>

using namespace std;

class A {
  private:
    template <typename T1, typename T2>
    class B { 
      private:
        function<T1>ff;

      public:
        B(T2 fun) {
          ff = bind(fun, 1, placeholders::_1);
        }

        virtual ~B() {
        }

        int exec(int x) {
          return ff(x);
        }
    };

    static int myFun(int x, int y) {
      return x + y;
    }

  public:
    A() { 
    };

    int test() {
      B<int(int), int (*)(int, int)> b(&myFun);
      return b.exec(10);
    }

    virtual ~A() {
    };
};

int main() {
  A a;

  // Correct result is '11' since we pass 11 and 1 is in the curried function.
  cout << "test result: " << a.test() << endl;

  return 0;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM