6

テンプレート化されていないクラスのテンプレート化されたメンバー関数を部分的に特殊化しようとしています:

#include <iostream>

template<class T>
class Foo {};

struct Bar {

  template<class T>
  int fct(T);

};

template<class FloatT>
int Bar::fct(Foo<FloatT>) {}


int main() {
  Bar bar;
  Foo<float> arg;
  std::cout << bar.fct(arg);
}

次のエラーが表示されます。

c.cc:14: error: prototype for ‘int Bar::fct(Foo<FloatT>)’ does not match any in class ‘Bar’
c.cc:9: error: candidate is: template<class T> int Bar::fct(T)

コンパイラ エラーを修正するにはどうすればよいですか?

4

1 に答える 1

9

関数 (メンバーまたはその他) の部分的な特殊化は許可されていません。

オーバーロードを使用:

struct Bar {

  template<class T>
  int fct(T data);

  template<class T>    //this is overload, not [partial] specialization
  int fct(Foo<T> data);

};
于 2012-11-08T19:25:02.317 に答える