成员函数的部分模板专用化:"prototype does not match"

Partial template specialization of member function: "prototype does not match"

本文关键字:prototype does match not 专用 函数 成员      更新时间:2023-10-16

我正在尝试部分专业化一个未模拟类的模板成员功能:

#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)

如何修复编译器错误?

不允许对功能的部分专业化(成员或其他方式)。

使用超载:

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