jeudi 16 juillet 2020

Template questions, specialization

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

Aucun commentaire:

Enregistrer un commentaire