C ++,函数指针指向模板函数指针 [英] C++, function pointer to the template function pointer

查看:540
本文介绍了C ++,函数指针指向模板函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个常用静态方法的指针

  class MyClass 
{
private:
static double(* pfunction)(const Object *,const Object *);
...
};

指向静态方法

  class SomeClass 
{
public:
static double getA(const Object * o1,const Object * o2);
...
};

初始化:

  double(* MyClass :: pfunction)(const Object * o1,const Object * o2)=& SomeClass :: getA; 

我想将此指针转换为静态模板函数指针:

 模板< class T> 
static T(* pfunction)(const Object< T> *,const Object< T& //编译错误

其中:

  class SomeClass 
{
public:
template< class T>
static double getA(const Object< T> * o1,const Object< T> * o2);
...
};但是存在以下编译错误:


错误:模板声明:T(* pfunction)(const Object< T> * o1,const Object< T> * o2)

解决方案

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.

这篇关于C ++,函数指针指向模板函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆