C + + STL generalization, full specialization, partial specialization

  1. Template specialization can be divided into two types by object type (class and global function): specialization of class template and specialization of global template function;
  2. It can be divided into full specialization and partial specialization according to the type of specialization (that is, only one or more template parameters can be selected for specialization). Partial specialization is not supported for the specialization of global template functions;
  3. The specialization of global template function needs to pay attention to several important elements: function return value function name parameter type, number and order, eg:
    template T max_(const T &, const T &)
  4. Class template specialization needs to pay attention to several important element class names, parameter types and numbers. The order of parameters is not important;
  5. Template specialization, any specialized version that further conditionally restricts the design of template parameters. General thinking
    Full specialization refers to all specialization, that is, specialization for all template parameters. <c++ primer>
    Partial specialization refers to partial specialization, that is to say, the specialization of some template parameters. <c++ primer>
    The definitions of full specialization and partial specialization are not very strict, so sometimes they are not easy to understand.
#include <iostream>
using namespace std;

namespace templateTest{

    //Template generalization
    template<typename T>
    class iterator_traits
    {
    public:
        iterator_traits()
        {
            cout << "Template generalization" << endl;
        }

        ~iterator_traits()
        {

        }
    };

    //Partial specialization
    template<typename T>
    class iterator_traits<T*>
    {
    public:
        iterator_traits()
        {
            cout << "Template partial specialization, specialized regular pointer" << endl;
        }

        ~iterator_traits()
        {

        }
    };

    //Partial specialization
    template<typename T>
    class iterator_traits<const T*>
    {
    public:
        iterator_traits()
        {
            cout << "Template partial specialization const Pointer" << endl;
        }

        ~iterator_traits()
        {

        }
    };

    //Total specialization
    template<>
    class iterator_traits<int>
    {
    public:
        iterator_traits()
        {
            cout << "Template full specialization int type" << endl;
        }

        ~iterator_traits()
        {

        }
    };
};

//generalization
template<class U, class T>
class Test
{
public:
    Test()
    {
        cout << "Test generalization" << endl;
    }
};

//Partial specialization
template< class T>
class Test<int,T>
{
public:

    Test()
    {
        cout << "Test Partial specialization" << endl;
    }
};

//Total specialization
template<>
class Test<int, char>
{
public:

    Test()
    {
        cout << "Test Total specialization" << endl;
    }
};
template<typename T>
void max(const T& t1, const T & t2)
{
    cout << "Template function generalization"<< endl;

}

//In fact, the function template does not have partial specialization, only full specialization
template<>
void max<int>(const int& t1, const int& t2)
{
    cout << "Template function specialization" << endl;
}

void main()
{
    templateTest::iterator_traits<int> t1;
    templateTest::iterator_traits<float> t2;
    templateTest::iterator_traits<int *> t3;
    templateTest::iterator_traits<const int *> t4;  
    Test<int, int> t5;
    Test<float, int> t6;
    Test<int, char> t7;
    max(5, 10);
    max(5.5, 10.5);
    system("pause");
}

The output is as follows:

Template full specialization int type
 Template generalization
 Template partial specialization, specialized regular pointer
 Template partial specialization, specialization const pointer
 Test partial specialization
 Test generalization
 Test specialization
 Template function specialization
 Template function generalization
 Please press any key to continue

Posted on Tue, 05 May 2020 08:57:53 -0400 by Garath531