web-dev-qa-db-ja.com

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)

ご協力いただきありがとうございます...

23
Ian

2番目のケースでは、getAは関数ではなく、関数templateであり、関数テンプレートへのポインターを持つことはできません。

あなたができることは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>;

しかし、pfunctionanyの可能なインスタンスgetAを指すようにする方法はないと思います。

18
icecrime

テンプレートはtemplate :)です。これは具象型ではなく、メンバーとして使用できません。例えば次のクラスは定義できません。

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

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

ここに A<int>は特殊なテンプレートなので、特殊なメンバーもいます

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

関数ポインタのテンプレートはC++では不正です。クラスの内側でも、単にクラスの外側でもかまいません。あなたはこれを書くことはできません(クラスの外でさえも):

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

このサンプルを参照してください: http://www.ideone.com/smh7

C++標準は$ 14/​​1で言っています、

テンプレートは、クラスまたは関数のファミリーを定義します。

「テンプレートはクラスのファミリーを定義します関数または関数ポインタ "。だからあなたがやろうとしていることは、許可されていないテンプレートを使って「関数ポインタのファミリー」を定義することです。

Loki ライブラリのGeneric Functorsは、あなたが抱えている種類の問題に対するエレガントなソリューションです。 :-)

8
Nawaz

あなたができることの1つは、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! 
2
Adel