0

Does anyone know why template<class T1, class T2> void A<T1, T2, 4>::f0() fails compilation? I would think it's a specialization?

Also are these understandings correct?

  1. For // A the values of the template parameters are used to build an argument list which is used to instantiate the template by defining the class as class A<A0, ..., An> {}.
  2. For // B, when the compiler needs to generate code for f0(), it searches template definitions for the class associated with the function. If a specialized version is found, that is used. If no specialized version is found, then the non specialized version A<T1, T2, I> is chosen in which the template parameters are deduced from the form.

Thanks!

// A
template<class T1, class T2, int I>
class A {
public:
    void f0();
};            // primary template

// B
template<class T1, class T2, int I>
void A<T1, T2, I>::f0()
{
    cout << "x" << endl;
}

template<class T1, class T2>
void A<T1, T2, 4>::f0()
{
    cout << "x" << endl;
}

template<>
void A<int, int, 4>::f0()
{
    cout << "z" << endl;
}

Compilation:

clang++ -std=c++11 -pedantic -Wall test176.cc && ./a.out
test176.cc:17:20: error: nested name specifier 'A<T1, T2, 4>::' for declaration does
      not refer into a class, class template or class template partial specialization
void A<T1, T2, 4>::f0()
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
notaorb
  • 1,944
  • 1
  • 8
  • 18
  • 1
    Does this answer your question? [C++ partial method specialization](https://stackoverflow.com/questions/1535816/c-partial-method-specialization) – Jorengarenar Jul 16 '20 at 18:15

2 Answers2

1

The main reason is because partial template class specialization have to be defined before declaring its functions. In particular, you would have to declare the specialized class before declaring its memeber functions:

template<class T1, class T2>
class A<T1,T2,4> {
public:
  void f0();
};

It's worth to notice that your last example, A<int,int,4>::f0(), is a (explicit) full class specialization which has different rules as the partial one and that's why that definition does not fail to compile.

SoilRos
  • 51
  • 5
0
template<class T1, class T2>
void A<T1, T2, 4>::f0()

requires specialized definition of:

template<class T1, class T2>
class A<T1, T2, 4> {};
notaorb
  • 1,944
  • 1
  • 8
  • 18