Если у вас есть шаблонный класс или шаблонная функция (или их комбинация), как вы привязываете эту функцию (сохраняя параметр типа шаблона)?
Мне дали некоторую помощь по основному синтаксису в посте ниже, чтобы привязать к функциям с явными параметрами типа шаблона, но потерять возможность предоставлять параметры типа шаблона в процессе.
Можно ли заставить это работать, чтобы по-прежнему можно было предоставлять параметры типа шаблона при будущих вызовах?
Много почистил этот код, но он, очевидно, не скомпилируется, потому что я не могу найти правильный синтаксис (есть ли способы сделать это)?
Удалено требование «вектор», чтобы упростить это:
Спасибо за помощь!
#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);
}
};
myParameter
? - person Joseph Mansfield   schedule 06.02.2013