C ++如何使用std :: bind / std :: function引用模板化函数

时间:2013-02-06 10:56:08

标签: c++ c++11 callback function-pointers std-function

如果你有模板化的类或模板化的函数(或两者的组合),你如何绑定该函数,(保留模板类型参数)?

我在下面的帖子中给出了一些关于基本语法的帮助,用显式模板类型参数绑定到函数,但是在这个过程中失去了提供模板类型参数的能力。

是否可以让它工作,以便仍然可以提供未来调用的模板类型参数?

清理了很多代码,但它显然不会编译,因为我无法找到正确的语法,(有没有办法做到这一点)?

删除"向量"要求简化这一点:

感谢您的帮助!

#include <functional>
#include <vector>
#include <string>

/***************************************/
template <typename CommandTemplateType>
class Storage
{
  public:
   // No idea how to define this vector to allow Template Parameters
   // static std::vector<std::function<void<ParameterTemplateType>
   //     (std::shared_ptr<ParameterTemplateType>)>> Functions;

   // I really don't need the collection, a single member would kick start my research:
   static std::function<void<ParameterTemplateType>(std::shared_ptr<ParameterTemplateType>)> Function;

  template <typename ParameterTemplateType>
  static void Execute(ParameterTemplateType parameter)
  {
     // Look up index, or loop through all.. 
     // I am trying to invoke the bound function with a template param:
     // Functions[index]<ParameterTemplateType>(parameter);
     // preferably, just:  
     Function<ParameterTempalteType>(parameter); 
  }
};

/***************************************/
template <typename TemplateType>
class MyClass
{

   template <typename ParameterTemplateType>
   void MyFunction(ParameterTemplateType myParameter)
   {
     // Do something; 
   }

   MyClass()
   {
      std::string parameter = L"Test String";

      // Do not know how to include the 
      // template<typename ParameterTemplateType> definition to bind call.
      // Storage::Functions.push_back(
      //     std::bind(&MyClass::MyFunction<ParameterTemplateType>,
//        this, std::placeholders::_1));

     // Or just something like:
     Storage::Function = std::bind(&MyClass::MyFunction<ParameterTemplateType>,
                             this, std::placeholders::_1));

      /***************************************/
      // Call the bound function with an explicit parameter somehow:
      std::string parameter = L"Test String";          
      Storage::Execute<std::string>(parameter);


   }
};

4 个答案:

答案 0 :(得分:11)

关键问题是在C ++ 11中你无法做到这样的事情:

// Doesn't compile
template <typename TemplateType>
static std::function<void(std::shared_ptr<TemplateType>)> Function;

类和函数可以是模板化的,但不是成员属性。

“魔术”是:

/*******************************************************************/
// Define a Function Pointer in a Container
class Storage
{
   template <typename TemplateType>
   struct FunctionContainer {
       static std::function<void(std::shared_ptr<TemplateType>)> Function;
   };
};
/*******************************************************************/
// Initialize FunctionContainer's Static Function Pointer if using static pointer.
template <typename TemplateType>
std::function<void(std::shared_ptr<TemplateType>)> Storage
    ::FunctionContainer<TemplateType>::Function;

然后,您可以将模板化函数绑定到此函数,如:

// Bind Function Pointer in Container to a Local Function
class MyClass
{
   template <typename TemplateType>
   void MyFunction(std::shared_ptr<TemplateType> parameter)
   {
     // Do something.
     // You can make this templated or non-templated.
   }
   MyClass()
   {
     // If you really want, you can templatize std::string in the following:
     Storage::FunctionContainer<std::string>::Function 
       = std::bind(&MyFunction<std::string>, this, std::placeholders::_1);
   }
}

您可以调用所有这些并提供模板化类型参数,如下所示:

//Invocation
std::shared_ptr<std::string> parameter;
parameter->get() = "Hello World".
Storage::FunctionContainer<std::string>::Function(parameter);

答案 1 :(得分:10)

模板类型替换完成后,std::function的模板参数应该是函数的签名。在您的情况下,TemplateTypeFunctionTemplateType都不会对成员函数MyFunction的签名产生影响 - 它将始终返回std::string并取一个std::string参数。因此,您要在std::function中存储的std::vector应为:

static std::vector<std::function<std::string(std::string)>> Functions;

回想一下,成员函数有一个隐含的第一个参数this。您需要将MyClass<...>::MyFunc<...>的第一个参数绑定到要调用它的对象。据推测,由于您在MyClass的构造函数中绑定了该函数,因此您希望该对象成为MyClass实例。这意味着您的push_back应如下所示:

Storage::Functions.push_back(
  std::bind(&MyClass<TemplateType>::MyFunction<int>, this,
    std::placeholders::_1)
);

现在,推入Functions的函数绑定到MyClass对象,并采用类型为std::string的单个参数。您可以像这样调用其中一个函数:

Storage::Functions[0]("something");

答案 2 :(得分:5)

如果我理解你的话......:)

您想要做的是不可能的,因为对于模板&lt; class T&gt; void foo(T)函数foo&lt; int&gt;()和foo&lt; double&gt;是不同类型的,你不能创建矢量持有指向这两个函数的指针,因为vector是同类容器。

为了克服这个问题,我们可以使用boost :: variant&lt;&gt;要么存储指向不同类型函数的指针,要么存储函数的参数。

template<class T> void foo(T);
typedef boost::variant<void (*)(int), void (*)(double)> func_ptr_variant;
std::vector<func_ptr_variant> v;
v.push_back(foo<int>);
v.push_back(foo<double>);

typedef boost::variant<int, double> argument;
std::vector<void (*)(argument)) v;
v.push_back(foo);
v.push_back(bar);
// foo and bar are defined as void foo(argument a) and void bar(argument a)

不幸的是,在任何情况下,您都需要在将函数模板插入容器之前对其进行实例化,因为C ++无法动态执行代码生成。我认为您可能知道函数可能使用的所有可能类型的参数,因此可能不是问题。

答案 3 :(得分:4)

MyClass的c-tor对FunctionTemplateType没有任何了解,这就是为什么它只能推送显式专用(对不起,这是我的用语......我不知道对了这个

#include <functional>
#include <vector>
#include <string>

struct Storage
{
  // Have no idea what this signature should really be:
  static std::vector<std::function<void ()>> Functions;

};
std::vector<std::function<void ()>> Storage::Functions;

template <typename TemplateType>
class MyClass
{
   template <typename FunctionTemplateType>
   std::string MyFunction(std::string myParameter)
   {
     return "Hellö: " + myParameter;

   }
public:
   MyClass()
   {
      Storage::Functions.push_back(
          std::bind( & MyClass<TemplateType>::MyFunction<std::string>, this, "borisbn" )
//                                                       ^^^^^^^^^^^
      );
   }
};

int main() {
    MyClass<int> obj;
}

liveworkspace link