Explicit specialized specialization of member functions

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?

+3


source to share


2 answers


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

Explicit specialization is declared in the namespace that encompasses the specialized pattern.



So, you need to move the specialization out of A

:

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

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

      

+1


source


From C ++ standards:

An explicit specialization is declared in a namespace, the template is a member or, for member templates, in the namespace of which the member class or the template of the enclosing class is a member. An explicit specialization of a member function, member class, or static data member of a class template must be declared in the namespace of which the class template is a member.



So, you need to move the specialization outside of definition A. You can try the following:

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

      

0


source







All Articles