[C ++ in-depth analysis] 45. Array class template

1 Numerical template parameters

The template parameter can be a numeric parameter. As shown below, the template parameter N is a numeric value.
Insert picture description here
Limitation of numerical template parameters

  • Variables, floating point numbers, class objects cannot be used as template parameters

Essence: The template parameters are the units that are processed during the compilation phase , therefore, they must be uniquely and accurately determined during the compilation phase.

Programming experiment: Calculate 1 + 2 + 3 + ... + N the most efficient way

// 45-1.cpp
#include<iostream>
using namespace std;

template<int N>
class Sum
{
public:
    static const int VALUE = Sum<N-1>::VALUE + N;
};

template<>				// 模板特化
class Sum<1>
{
public:
    static const int VALUE = 1;
};

int main()
{
    cout << "1+2+3+...+10 = " << Sum<10>::VALUE << endl;
    cout << "1+2+3+...+100 = " << Sum<100>::VALUE << endl;
    return 0;
}

Use a numeric template parameter to define the class template, and VALUE is defined recursively, which is equal to Sum :: VALUE + N; then use template specialization to define the VALUE value when N is 1.

The specific class is generated based on the specific N value. At compile time, the VALUE in the class has been determined, so the runtime is just taking a value without calculation, and the fastest.

Compile and run:

$ g++ 45-1.cpp -o 45-1
$ ./45-1
1+2+3+...+10 = 55
1+2+3+...+100 = 5050

2 Array template class

// Array.h
#ifndef _ARRAY_H_
#define _ARRAY_H_
template<typename T, int N>
class Array
{
    T m_array[N];
public:
    int length();
    bool set(int index, T value);
    bool get(int index, T& value);
    T& operator[] (int index);
    T operator[] (int index) const;
    virtual ~Array();
};

template<typename T, int N>
int Array<T, N>::length()
{
    return N;
}

template<typename T, int N>
bool Array<T, N>::set(int index, T value)
{
    bool ret = (index >= 0) && (index < N);
    if (ret)
    {
        m_array[index] = value;
    }
    return ret;
}

template<typename T, int N>
bool Array<T, N>::get(int index, T& value)
{
    bool ret = (index >= 0) && (index < N);
    if (ret)
    {
        value = m_array[index];
    }
    return ret;
}
template<typename T, int N>
T& Array<T, N>::operator[] (int index)
{
    return m_array[index];
}

template<typename T, int N>
T Array<T, N>::operator[] (int index) const
{
    return m_array[index]; 
}
template<typename T, int N>
Array<T, N>:: ~Array()
{

}
#endif
// 45-2.cpp
#include<iostream>
#include"Array.h"
using namespace std;
int main()
{
    Array<char, 10>a;
    for (int i = 0; i < 10; i++)
    {
        a[i] = 'a' +  i;
    }
    for (int i = 0; i < 10; i++)
    {
        cout << a[i] << ", ";
    }
    cout << endl;
    return 0;
}

Use the template and numeric template parameters to define the array class template.

$ g++ 45-2.cpp -o 45-2
$ ./45-2
a, b, c, d, e, f, g, h, i, j, 

3 Array class transformation

In [C ++ deep analysis] 12, constructor, copy constructor and destructor , we define the array class, and add copy constructor to achieve deep copy. In order to prevent the generation of semi-finished objects, in [C ++ in-depth analysis] 17, second-order construction mode to improve the second-order construction. Here we use class templates for transformation.

// HeapArray.h
#ifndef _HEAPARRAY_H_
#define _HEAPARRAY_H_
template<typename T>
class HeapArray
{
private:
    HeapArray(int len);
    HeapArray(const HeapArray& obj);
    bool construct();
    int m_length;
    T* m_pointer;
public:
    static HeapArray<T>* NewInstance(int length);
    int length();
    bool get(int index, T& value);
    bool set(int index, T value);
    T& operator [] (int index);
    T operator [] (int index) const;
    HeapArray<T>& self();
    ~HeapArray();
};

template<typename T>
HeapArray<T>::HeapArray(int len)
{
    m_length = len;
}

template<typename T>
bool HeapArray<T>::construct()
{
    m_pointer = new T[m_length];
    return m_pointer != NULL;
}

template<typename T>
 HeapArray<T>* HeapArray<T>::NewInstance(int length)
{
    HeapArray<T>* ret = new HeapArray<T>(length);
    if (!(ret != NULL && ret->construct()))
    {
        delete ret;
        ret = NULL;
    }
    return ret;
}

template<typename T>
int HeapArray<T>::length()
{
    return m_length;
}

template<typename T>
bool HeapArray<T>::get(int index, T& value)
{
    bool ret = (index >= 0) && (index < m_length);
    if (ret)
    {
        value = m_pointer[index];
    }
    return ret;
}

template<typename T>
bool HeapArray<T>::set(int index, T value)
{
    bool ret = (index >= 0) && (index < m_length);
    if (ret)
    {
        m_pointer[index] = value;
    }
    return ret;
}

template<typename T>
T& HeapArray<T>::operator [] (int index)
{
    return m_pointer[index];
}

template<typename T>
T HeapArray<T>::operator [] (int index) const
{
    return m_pointer[index];
}

template<typename T>
HeapArray<T>& HeapArray<T>::self()
{
    return *this;
}

template<typename T>
HeapArray<T>::~HeapArray()
{
    delete []m_pointer;
}
#endif
// 45-3.cpp
#include<iostream>
#include"HeapArray.h"
using namespace std;
int main()
{
    HeapArray<char>* pa = HeapArray<char>::NewInstance(10);
    if (pa != NULL)
    {
        HeapArray<char>& ai = pa->self();
        for (int i = 0; i < ai.length(); i++)
        {
            ai[i] = 'a' + i;
        }
        for (int i = 0; i < ai.length(); i++)
        {
            cout << ai[i] << ", ";
        }
        cout << endl;
    }
    return 0;
}
$ g++ 45-3.cpp -o 45-3
$ ./45-3
a, b, c, d, e, f, g, h, i, j, 

4 Summary

1. The template parameter can be a numeric parameter
2. The numeric template parameter must be uniquely determined during compilation

Published 298 original articles · praised 181 · 100,000+ views

Guess you like

Origin blog.csdn.net/happyjacob/article/details/104569560