且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++,函数指针指向模板函数指针

更新时间:2022-10-15 12:33:43

p>在第二种情况下, getA 不是函数,而是函数 template ,并且不能有指向函数模板的指针。

你可以做的是有 pfunction 指向一个特定的 getA instance (例如: 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>

但我不认为有办法获得 code>指向 getA 的任何可能实例。


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

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.