C ++如何使用std :: bind / std :: function来引用模板函数 [英] C++ How to Reference Templated Functions using std::bind / std::function

查看:226
本文介绍了C ++如何使用std :: bind / std :: function来引用模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If you have a templated class or a templated function, (or combination of the two), how do you bind that function, (preserving the template type parameter)?

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

I was given some help about the basic syntax in a post below, to bind to functions with explicit template type parameters, but lose the ability to provide template type parameters in the process.

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

Is it possible to get this to work so that it is still possible to provide template type parameters with future calls?

清理了很多代码,但是它显然不会编译,因为我找不到正确的语法,(是否有任何方法这样做)?

Cleaned up this code a lot, but it obviously won't compile because I can't find the correct syntax, (are there any ways of doing this)?

删除向量要求简化:

感谢您的帮助!

#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);


   }
};


推荐答案

关键问题是,在C ++ 11中不能像下面这样做:

The key issues is that in C++11 you cannot do something like:

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

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

Classes and Functions can be templated, but not member properties.

魔法是:

/*******************************************************************/
// 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;

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

You can then Bind a templated function to this function like:

// 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);
   }
}

您可以调用所有这些并提供模板类型参数:

And you can invoke all of this and provide a templated type parameter like so:

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

这篇关于C ++如何使用std :: bind / std :: function来引用模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆