Deep analysis of [C++] 59_class template

Posted by ted_chou12 on Sat, 11 May 2019 06:45:35 +0200

Multi-parameter class template

  • Class templates can define any number of different type parameters

template < typename T1, typename T2 >
class Test
{
public:
    void add(T1 a, T2 b);
};

==>

Test<int float> t;

  • Class templates can be specialized

    • Specific implementation of specified class template
    • Some type parameters must display the specified
    • Implementing class templates separately according to type parameters

  • Specification Types of Class Templates

    • Partial Specialization - Constraining Type Parameters with Specific Rules
    • Full Specialization - Full Display Specified Type Parameters

template 
< typename T1, typename T2 >
class Test
{

};

Partial specialization==>

template 
< typename T >
class Test <T, T>
{

};

Complete specialization==>

template
< >
class Test <int , int >
{

}

Programming Experiments: Specification of Class Templates

#include <iostream>

using namespace std;

template 
< typename T1, typename T2 >
class Test
{
public:
    void add(T1 a, T2 b)
    {
        cout << "void add(T1 a, T2 b)" << endl;
        cout << a + b << endl;
    }
};

template
< typename T >
class Test < T, T >                 // When two types of parameters are the same          
{
public:
    void add(T a, T b)
    {
        cout << "void add(T a, T b)" << endl;
        cout << a + b << endl;
    }
    void print()
    {
        cout << "class Test < T, T >" << endl;
    }
};

template 
< typename T1, typename T2 >       // When the first type parameter is pointer and the second type parameter is pointer
class Test < T1*, T2*>
{
public:
    void add(T1* a, T2* b)
    {
        cout << "void add(T1* a, T2* b)" << endl;
        cout << *a + *b << endl;
    }
};

template 
<  >
class Test < void*, void* >        // When T1 = void*, T2 = void*
{
public:
    void add(void* a, void* b)
    {
        cout << "void add(void* a, void* b)" << endl;
        cout << "Error to add* param..." << endl;
    }
};


int main()
{
    Test<int, float> t1;
    Test<long, long> t2;
    Test<void*, void*> t3;
    
    t1.add(1, 2.5);
    
    t2.add(5, 5);
    t2.print();
    
    t3.add(NULL, NULL);
    
    Test<int*, double*> t4;
    int a = 1;
    double b = 0.1;
    
    t4.add(&a, &b);

    return 0;
}
Output:
void add(T1 a, T2 b)
3.5
void add(T a, T b)
10
class Test < T, T >
void add(void* a, void* b)
Error to add* param...
void add(T1* a, T2* b)
1.1

  • Notes for Class Template Specialization

    • Specialization is just a separate implementation of templates

      • Essentially the same template
    • The use of specialized class templates is uniform

      • Each type parameter specified must be displayed

Question:
Is there a difference between class template specialization and redefinition?
Can function templates be specialized?

Deep analysis of specialization

  • Redefinition and specialization are different

    • redefinition

      • A class template and a new class (or two class templates)
      • When using it, we need to consider how to choose it.
    • Specialization

      • Use class templates and feature classes in a unified way
      • The compiler prefers the specialization class
  • Function templates only support full specialization of type parameters

template 
< typename T >          // Function template definition
bool Equal(T a, T b)
{
    return a == b;
}

template
< >                     // Full specialization of function template
bool Equal<void*>(void* a, void* b)
{
    return a == b;
}

Programming Experiments: Deep Understanding of Specialization

#include <iostream>

using namespace std;

template 
< typename T1, typename T2 >
class Test
{
public:
    void add(T1 a, T2 b)
    {
        cout << "void add(T1 a, T2 b)" << endl;
        cout << a + b << endl;
    }
};
/*
template 
<  >
class Test < void*, void* >        // T1 == void* , T2 == void* time
{
public:
    void add(void* a, void* b)
    {
        cout << "void add(void* a, void* b)" << endl;
        cout << "Error to add* param..." << endl;
    }
};
*/

class Test_Void
{
public:
    void add(void* a, void* b)
    {
        cout << "void add(void* a, void* b)" << endl;
        cout << "Error to add* param..." << endl;
    }
};

template
<typename T>
bool Equal(T a, T b)
{
    cout << "bool Equal(T a, T b)" << endl;
    
    return a == b;
}

template
< >
bool Equal<double>(double a, double b)
{
    const double delta = 0.00000000001;
    double r = a - b;
    
    cout << "bool Equal<double>(double a, double b)" << endl;
    
    return (-delta < r) && (r < delta);
}

bool Equal(double a, double b)
{
    const double delta = 0.00000000001;
    double r = a - b;
    
    cout << "bool Equal(double a, double b)" << endl;
    
    return (-delta < r) && (r < delta);
}

int main()
{
    cout << Equal(1, 1) << endl;
    cout << Equal(0.1, 0.1) << endl;
    cout << Equal<>(0.1, 0.1) << endl;

    return 0;
}
Output:
bool Equal(T a, T b)
1
bool Equal(double a, double b)
1
bool Equal<double>(double a, double b)
1

Suggestions in Engineering

When overloading function templates is needed, priority should be given to template specialization.
When template specialization can not meet the requirements, then use function overload!

Summary

  • Class templates can define any number of different type parameters
  • Class templates can be partially and completely specialized
  • The essence of specialization is the separate implementation of templates
  • Function templates only support full specialization
  • Use template specialization instead of class (function) redefinition in Engineering

The above content refers to Ditai Software College series courses, please protect the original!

Topics: C++ Programming