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

发布于 2024-10-10 13:01:39 字数 971 浏览 4 评论 0原文

我有一个指向公共静态方法的指针,

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;

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

template <class T>
static T ( *pfunction ) ( const Object <T> *, const Object <T> *); //Compile error

其中:

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

但是出现以下编译错误:

error: template declaration of : T (* pfunction )(const Object <T> *o1, const Object <T> *o2)

感谢您的帮助...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧夏天 2024-10-17 13:01:39

在第二种情况下,getA 不再是一个函数,而是一个函数模板,并且您不能拥有指向函数模板的指针。

您可以做的是让 pfunction 指向特定的 getA 实例(即:对于 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>;

但我认为没有办法让 pfunction 指向 getA任何可能的实例。

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.

北音执念 2024-10-17 13:01:39

template 是一个模板 :) 它不是具体类型,不能用作成员。例如,您不能定义以下类:

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

因为 template; std::vectormember; 是可以专门化为许多不同类型的东西。你可以这样做:

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

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

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

这里 A 是一个专门的模板,因此有专门的成员

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

你又不是我 2024-10-17 13:01:39
template <class T>
static T ( *pfunction ) ( const Object <T> *, const Object <T> *);

函数指针模板在 C++ 中是非法的。无论是在课堂内,还是在课堂外。您不能这样写(即使在课堂之外也不能):

template <class X>
void (*PtrToFunction) (X);

请参阅此示例: http://www.ideone.com/smh73

C++ 标准以 $14/1 表示,

模板定义一系列
函数

请注意,它并没有说“模板定义了一系列函数或函数指针”。所以你想要做的是,使用模板定义“一系列函数指针”,这是不允许的。

Loki 库中的通用函子将是解决您遇到的此类问题的优雅解决方案。 :-)

template <class T>
static T ( *pfunction ) ( const Object <T> *, const Object <T> *);

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):

template <class X>
void (*PtrToFunction) (X);

See this sample : http://www.ideone.com/smh73

The C++ Standard says in $14/1,

A template defines a family of classes
or functions.

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

未蓝澄海的烟 2024-10-17 13:01:39

您可以做的一件事是在 cpp 文件中拥有模板成员函数的副本并指向该副本,即

template <+typename ElementType>
int PQueueHeap<ElementType>::compareFunction(ElementType First,ElementType Second)
{   
    if (First>Second) return 1; else if (First==Second) return 0; else return -1;
}

// you cannot point to above 

您可以指向

template <+typename ElementType>

int compareFunction(ElementType First,ElementType Second)
{

if (First>Second) return 1; else if (First==Second) return 0; else return -1;
} // No error and it works! 

One thing you can do is have a copy of the template member function in the cpp file and point to that i.e.

template <+typename ElementType>
int PQueueHeap<ElementType>::compareFunction(ElementType First,ElementType Second)
{   
    if (First>Second) return 1; else if (First==Second) return 0; else return -1;
}

// you cannot point to above 

however you can point to

template <+typename ElementType>

int compareFunction(ElementType First,ElementType Second)
{

if (First>Second) return 1; else if (First==Second) return 0; else return -1;
} // No error and it works! 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文