如果你有一个模板类或模板函数,(或两者的组合),你如何绑定(bind)那个函数,(保留模板类型参数)?
我在下面的帖子中获得了一些关于基本语法的帮助,以绑定(bind)到具有显式模板类型参数的函数,但在此过程中失去了提供模板类型参数的能力。
是否有可能让它工作,以便仍然可以为将来的调用提供模板类型参数?
这段代码清理了很多,但显然无法编译,因为我找不到正确的语法,(有没有办法这样做)?
删除了“vector ”要求以简化此操作:
感谢您的帮助!
#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 中你不能这样做:
// 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)到此函数,例如:
// 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);
关于C++ 如何使用 std::bind/std::function 引用模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14727313/