I am having a pointer to the common static method
class MyClass
{
private:
static double ( *pfunction ) ( const Object *, const Object *);
...
};
pointing to the static method
class SomeClass
{
public:
static double getA ( const Object *o1, const Object *o2);
...
};
Initialization:
double ( *MyClass::pfunction ) ( const Object *o1, const Object *o2 ) = &SomeClass::getA;
I would like to convert this pointer to the static template function pointer:
template <class T>
static T ( *pfunction ) ( const Object <T> *, const Object <T> *); //Compile error
where:
class SomeClass
{
public:
template <class T>
static double getA ( const Object <T> *o1, const Object <T> *o2);
...
};
But there is the following compile error:
error: template declaration of : T (* pfunction )(const Object <T> *o1, const Object <T> *o2)
Thanks for your help...
template is a template :) it's not a concrete type and cannot be used as a member. e.g. you cannot define following class:
because
template <class T> std::vector<T> member;
is something that potentially can be specialized to many different types. you can do something like this:here
A<int>
is a specialized template and so has specialized memberOne thing you can do is have a copy of the template member function in the cpp file and point to that i.e.
however you can point to
In the second case,
getA
is not a function anymore but a function template, and you can't have a pointer to function template.What you can do is have
pfunction
point to a particulargetA
instance (ie: forT = int
) :But I don't think there is a way to get
pfunction
to point on any possible instance ofgetA
.Template of function pointer is illegal in C++. Be it inside a class, or simply outside a class. You cannot write this (not even outside a class):
See this sample : http://www.ideone.com/smh73
The C++ Standard says in $14/1,
Please note that it does NOT say "A template defines a family of classes, functions or function pointers". So what you're trying to do is, defining "a family of function pointers" using template, which isn't allowed.
Generic Functors from Loki library would be an elegant solution to the kind of problem you're having. :-)