Frank
Frank

Reputation: 66144

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?

Upvotes: 6

Views: 819

Answers (1)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361252

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

};

Upvotes: 9

Related Questions