Use of full specialization and partial specialization of C + + class template and function template

Time:2022-1-14

1、 Full specialization and partial specialization of class template

#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U>
class TC
{
public:
 TC() 
 {
 STD:: cout < < generalized version constructor < < STD:: endl;
 }
 void funtest()
 {
 STD:: cout < < generalized version member function < < STD:: endl;
 }
};
 
template<>
class TC<int, int>
{
public:
 TC()
 {
 STD:: cout < < fully specialized version constructor < < STD:: endl;
 }
 void funtest()
 {
 STD:: cout < < fully specialized version member function < < STD:: endl;
 }
};
 
template<>
void TC<double, double>::funtest()
{
 STD:: cout < < fully specialized version function < < STD:: endl;
 
}

main.cpp


#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
 TC<char, int> tchar;
 tchar.funtest();
 TC<int, int> tint;
 tint.funtest();
 TC<double, double> tdouble;
 tdouble.funtest();
}

Output:

Generalized version constructor
Generalized version member function
Fully specialized version constructor
Fully specialized version member function
Generalized version constructor
Fully specialized version function

2、 Partial specialization of class template

1. Number of template parameters:

template.h

#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U, typename W>
class TC2
{
public:
 void funtest()
 {
 STD:: cout < < generalized version member function < < STD:: endl;
 }
};
 
template <typename U>
class TC2<int, U, double>
{
public:
 void funtest()
 {
 STD:: cout < < partial specialization version member function < < STD:: endl;
 }
};

main.cpp


#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
 TC2<double, double, double> tdouble2;
 tdouble2.funtest();
 TC2<int, double, double> tint2;
 tint2.funtest()
}

Output:

Generalized version member function
Partial specialization version member function

2. From template parameter range:

template.h

#pragma once
#include <iostream>
#include <map>
 
template <typename T>
class TC3
{
public:
 void funtest()
 {
 STD:: cout < < generalized version member function < < STD:: endl;
 }
};
 
template <typename T>
class TC3<const T>
{
public:
 void funtest()
 {
 STD:: cout < < const t partial specialization version member function < < STD:: endl;
 }
};
 
template <typename T>
class TC3<T&>
{
public:
 void funtest()
 {
 STD:: cout < < T & partial specialization version member function < < STD:: endl;
 }
};
 
template <typename T>
class TC3<T *>
{
public:
 void funtest()
 {
 STD:: cout < < T * partial specialization version member function < < STD:: endl;
 }
};

main.cpp


#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
 TC3<int> tint3;
 tint3.funtest();
 TC3<int &> tint3_ref;
 tint3_ref.funtest();
 TC3<int *> tint3_point;
 tint3_point.funtest();
 TC3<const int> tint3_const;
 tint3_const.funtest();
}

Output:

Generalized version member function
T & partial specialization version member function
T * partial specialization version member function
Const t partial specialization version member function

3、 Full specialization of function template (partial specialization is not allowed)

template.h

#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U>
void tfunc(T& a, U& b)
{
 STD:: cout < < tfunc generalized version function < < STD:: endl;
}
 
template <>
void tfunc(int& a, int& b)
{
 STD:: cout < < tfunc fully specialized version function < < STD:: endl;
}

main.cpp


#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
 int a1 = 1;
 double b1 = 3.2;
 tfunc(a1, b1);
 tfunc(a1, a1);
}

Output:

Tfunc generalized version function
Tfunc fully specialized version function

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.