Use of C++ class templates function templates with full specialization and partial specialization

  • 2020-06-23 01:40:09
  • OfStack

1. Full specialization and partial specialization of class templates


#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U>
class TC
{
public:
 TC() 
 {
 std::cout << " Generalized version constructors " << std::endl;
 }
 void funtest()
 {
 std::cout << " Generalizes version member functions " << 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 functions " << std::endl;
 }
};
 
template<>
void TC<double, double>::funtest()
{
 std::cout << " Fully specialized version functions " << 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 constructors
Generalizes version member functions
Fully specialized version constructor
Fully specialized version member functions
Generalized version constructors
Fully specialized version functions

]

2. Partial specialization of class templates

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 << " Generalizes version member functions " << std::endl;
 }
};
 
template <typename U>
class TC2<int, U, double>
{
public:
 void funtest()
 {
 std::cout << " Partial specialized version member functions " << 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:

[

Generalizes version member functions
Partial specialized version member functions

]

2. Parameters from the template:

template.h


#pragma once
#include <iostream>
#include <map>
 
template <typename T>
class TC3
{
public:
 void funtest()
 {
 std::cout << " Generalizes version member functions " << std::endl;
 }
};
 
template <typename T>
class TC3<const T>
{
public:
 void funtest()
 {
 std::cout << "const T Partial specialized version member functions " << std::endl;
 }
};
 
template <typename T>
class TC3<T&>
{
public:
 void funtest()
 {
 std::cout << "T& Partial specialized version member functions " << std::endl;
 }
};
 
template <typename T>
class TC3<T *>
{
public:
 void funtest()
 {
 std::cout << "T * Partial specialized version member functions " << 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:

[

Generalizes version member functions
T & Partial specialized version member functions
T * partial specialized version member functions
const T partial specialized version member functions

]

3. Full specialization of function template (not partial specialization)

template.h


#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U>
void tfunc(T& a, U& b)
{
 std::cout << "tfunc  Generalize version functions " << std::endl;
}
 
template <>
void tfunc(int& a, int& b)
{
 std::cout << "tfunc  Fully specialized version functions " << 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 generalizes version functions
tfunc fully specialized version function

]

Related articles: