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;
我想将此指针转换为静态模板函数指针:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在第二种情况下,
getA
不再是一个函数,而是一个函数模板,并且您不能拥有指向函数模板的指针。您可以做的是让
pfunction
指向特定的getA
实例(即:对于T = 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 particulargetA
instance (ie: forT = int
) :But I don't think there is a way to get
pfunction
to point on any possible instance ofgetA
.template 是一个模板 :) 它不是具体类型,不能用作成员。例如,您不能定义以下类:
因为
template; std::vectormember;
是可以专门化为许多不同类型的东西。你可以这样做:这里
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:
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:here
A<int>
is a specialized template and so has specialized member函数指针模板在 C++ 中是非法的。无论是在课堂内,还是在课堂外。您不能这样写(即使在课堂之外也不能):
请参阅此示例: http://www.ideone.com/smh73
C++ 标准以 $14/1 表示,
请注意,它并没有说“模板定义了一系列类、函数或函数指针”。所以你想要做的是,使用模板定义“一系列函数指针”,这是不允许的。
Loki 库中的通用函子将是解决您遇到的此类问题的优雅解决方案。 :-)
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):
See this sample : http://www.ideone.com/smh73
The C++ Standard says in $14/1,
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. :-)
您可以做的一件事是在 cpp 文件中拥有模板成员函数的副本并指向该副本,即
您可以指向
One thing you can do is have a copy of the template member function in the cpp file and point to that i.e.
however you can point to