Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, function pointer to the template function pointer

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...

like image 960
Ian Avatar asked Jan 01 '11 11:01

Ian


People also ask

How do you use a pointer to a function in C?

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.

Can we use pointer with function in C?

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.

How do you print a pointer to a function?

You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.


2 Answers

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 particular getA instance (ie: for T = int) :

class MyClass {     static double (*pfunction)(const Object<int> *, const Object<int> *); };  double (*MyClass::pfunction)(const Object<int> *o1, const Object<int> *o2)  = &SomeClass::getA<int>; 

But I don't think there is a way to get pfunction to point on any possible instance of getA.

like image 184
icecrime Avatar answered Sep 23 '22 14:09

icecrime


template is a template :) it's not a concrete type and cannot be used as a member. e.g. you cannot define following class:

class A
{
    template <class T> std::vector<T> member;
}

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:

template <class T>
struct A
{
 static T (*pfunction)();
};

struct B
{
 template <class T>
 static T getT();
};

int (*A<int>::pfunction)() = &B::getT<int>;

here A<int> is a specialized template and so has specialized member

like image 38
Andriy Tylychko Avatar answered Sep 23 '22 14:09

Andriy Tylychko