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)?
Removed the "vector" requirement to simplify this:
Thanks for the help!
#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);
}
};
MyClass's c-tor doesn't know anything about
FunctionTemplateType
that's why it can push_back only explicit specialized (sorry, it's term of mine... I don't know the right term) like thisliveworkspace link
The key issues is that in C++11 you cannot do something like:
Classes and Functions can be templated, but not member properties.
The "Magic" is:
You can then Bind a templated function to this function like:
And you can invoke all of this and provide a templated type parameter like so:
The template argument for
std::function
should be the signature of the function after template type substitution has been done. In your case, neitherTemplateType
norFunctionTemplateType
have an effect on the signature of the member functionMyFunction
- it will always return astd::string
and take a singlestd::string
argument. Therefore, thestd::function
you're going to store in yourstd::vector
should be:Recall that a member function has an implicit first argument
this
. You need to bind the first argument ofMyClass<...>::MyFunc<...>
to the object you want it to be called on. Presumably, since you're binding the function inMyClass
's constructor, you want the object to be thatMyClass
instance. That means yourpush_back
should look like this:Now the function that is pushed into
Functions
is bound to yourMyClass
object and takes a single argument of typestd::string
. You can call one of these functions like so:If I understood you right... :)
What you want to do is not possible because for template<class T> void foo(T) functions foo<int>() and foo<double> are of different types and you can not create vector holding pointers to both that functions directly because vector is homogeneous container.
To overcome that we can use boost::variant<> to either store pointers to different types of functions or to store arguments of functions.
Unfortunately, in any case you will need to instantiate function templates before inserting them to container because C++ can not do code generation on the fly. I think that it is possible that you know all possible types of arguments that the function may be used with so that may be not a problem.