Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to partially specialize a templated member function of an untemplated class:

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

I'm getting the following error:

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)

How can I fix the compiler error?

like image 997
Frank Avatar asked Nov 08 '12 19:11

Frank


1 Answers

Partial specialization of functions (member or otherwise) is not allowed.

Use overload:

struct Bar {

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

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

};
like image 164
Nawaz Avatar answered Nov 07 '22 07:11

Nawaz