【问题标题】:C++, function pointer to the template function pointerC++,函数指针指向模板函数指针
【发布时间】:2011-01-01 11:43:24
【问题描述】:

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

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)

感谢您的帮助...

【问题讨论】:

    标签: c++ templates function-pointers


    【解决方案1】:

    在第二种情况下,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 实例。

    【讨论】:

      【解决方案2】:

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

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

      因为template &lt;class T&gt; std::vector&lt;T&gt; member; 可能会专门用于许多不同的类型。你可以这样做:

      template <class T>
      struct A
      {
       static T (*pfunction)();
      };
      
      struct B
      {
       template <class T>
       static T getT();
      };
      
      int (*A<int>::pfunction)() = &B::getT<int>;
      

      这里A&lt;int&gt; 是一个专门的模板,因此也有专门的成员

      【讨论】:

        【解决方案3】:
        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 库中的通用仿函数将是解决您遇到的这类问题的优雅解决方案。 :-)

        【讨论】:

          【解决方案4】:

          您可以做的一件事是在 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! 
          

          【讨论】: