C++,函数指针指向模板函数指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4573941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++, function pointer to the template function pointer
提问by Ian
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...
谢谢你的帮助...
回答by icecrime
In the second case, getA
is not a function anymore but a function template, and you can't have a pointer to function template.
在第二种情况下,getA
不再是函数而是函数模板,并且您不能拥有指向函数模板的指针。
What you can do is have pfunction
point to a particular getA
instance(ie: for T = int
) :
您可以做的是pfunction
指向特定getA
实例(即: 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 anypossible instance of getA
.
但我认为没有办法pfunction
指出任何可能的getA
.
回答by Andriy Tylychko
template is a template:) it's not a concrete type and cannot be used as a member. e.g. you cannot define following class:
template 是一个模板:) 它不是一个具体的类型,不能用作成员。例如,您不能定义以下类:
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> std::vector<T> member;
是潜在可以专门用于许多不同类型的东西。你可以这样做:
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
这A<int>
是一个专门的模板,所以有专门的成员
回答by Nawaz
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):
函数指针模板在 C++ 中是非法的。无论是在课堂内,还是在课堂外。你不能写这个(甚至不能在课堂外):
template <class X>
void (*PtrToFunction) (X);
See this sample : http://www.ideone.com/smh73
请参阅此示例:http: //www.ideone.com/smh73
The C++ Standard says in $14/1,
C++ 标准以 14 美元/1 美元表示,
A template defines a family of classesor functions.
模板定义了一系列类或函数。
Please note that it does NOT say "A template defines a family of classes, functionsor 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 Lokilibrary would be an elegant solution to the kind of problem you're having. :-)
Loki库中的泛型函子将是您遇到的这类问题的优雅解决方案。:-)
回答by Adel
One thing you can do is have a copy of the template member function in the cpp file and point to that i.e.
您可以做的一件事是在 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
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!