Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit template specialization of member functions [duplicate]

Tags:

c++

templates

I have the following minimal example:

class A
{
  template<typename X, typename Y>
  void f()
  { }

  template<>
  void f<int, char>()
  { }
};

The compiler gives an error message

 explicit specialzation in non-namespace scope.

Why is this wrong and how can I fix it?

like image 933
user2683038 Avatar asked Nov 11 '22 02:11

user2683038


1 Answers

§14.7.3 [temp.expl.spec]/p2:

An explicit specialization shall be declared in a namespace enclosing the specialized template.

So you need to move the specialization outside A's definition:

class A
{
  template<typename X, typename Y>
  void f()
  { }
};

template<>
void A::f<int, char>()
{ }
like image 195
T.C. Avatar answered Nov 15 '22 13:11

T.C.