[C++] What is a function template/class template?


1. Function template

insert image description here

1. What is a function template?

Simply put, a function template is a template, which has nothing to do with the type of the function parameter. It is a mold, not a real function. The instantiated function will automatically deduce the type according to the type of the actual parameter .

2. Function template format

template<typename T1,typename T2...>

返回值类型 函数名(参数列表)
{
    
    }

3. Function template principle

A function template is a template, not a real function. It instantiates a specific function according to the type of the passed actual parameter, which is equivalent to handing over the repeated things to the compiler.

For example:

template<typename T>

T Add(const T& a1, const T& a2)
{
    
    
    return a1 + a2;
}


int main()
{
    
    
    int a = 10, b = 20;
    double c = 3.14,d = 2.11;
    cout << Add(a, b) << endl;
    cout << Add(c, d) << endl;	
    return 0;
}

We define a function template of the addition function to perform the addition operation. The function template will automatically instantiate different Add functions according to the type of the actual parameter we pass, and these functions constitute function overloading .

4. Function template instantiation

(1) Implicit instantiation

To give a simple example:
In the function template we define, only one template parameter type is given, but we may have two different actual parameter types.

template<typename T>

T Add(const T& a1, const T& a2)
{
    
    
    return a1 + a2;
}


int main()
{
    
    
    int a = 10;
    double b = 20.3;
    cout << Add(a, b) << endl;

    return 0;
}

For a function template with only one type, but two different types of actual parameters appear at the same time, the compiler cannot confirm which actual parameter type is used for instantiation.

Solution:

int main()
{
    
    
    int a = 10;
    double b = 20.3;
    cout << Add((double)a, b) << endl;
	
	cout << Add(a, (int)b) << endl;
	//可能存在精度丢失的情况
    return 0;
}

Implicit type conversion can solve the situation that the compiler cannot recognize.

(2) Display instantiation

template<typename T>

T Add(const T& a1, const T& a2)
{
    
    
    return a1 + a2;
}


int main()
{
    
    
    int a = 10;
    double b = 20.3;
    cout << Add(a, b) << endl;

    return 0;
}

For this case, there is another solution:

int main()
{
    
    
    int a = 10;
    double b = 20.3;
    cout << Add<int>(a, b) << endl;

    return 0;
}

Such a method is called explicit instantiation. In real life, we rarely encounter situations that require explicit instantiation, but for the following situations, explicit strength is necessary.

template<class T>
T* Alloc(int n)
{
    
    
    return new T[n];
}

int main()
{
    
    
    Alloc<double>(10);
    return 0;
}

In this case, we are not using a function template type, so just passing the argument n cannot deduce the type of T, in which case explicit instantiation must be used.

2. Class template

1. Class template definition format

template<class T1, class T2, ..., class Tn>
class 类模板名
{
    
    
	// 类内成员定义
};

2. Instantiation of class templates

Notice:

The type and class name of a normal class are the same while
the type and class name of a template class are different

Class template instantiation is different from function template instantiation. Class template instantiation needs to follow the class template name with <>, and then put the instantiated type in <>.

The class template name is not the real class, but the result of instantiation is the real class.

for example:

// string类名,string<int>才是类型
string<int> s1;
string<double> s2;

1. Whether it is a class template or a function template, its scope of action is the immediately following class/function.

That is, look at {}

2. When a function in a class template is defined outside the class, a template parameter list needs to be added.
For example:

template<typename T>
class Stack
{
    
    
public:
    Stack(size_t capacity = 3);

    void Push(const T& data);   

    // 其他方法...
    ~Stack()
    {
    
    
        if (_array)
        {
    
    
            delete[]_array;
            _capacity = _size = 0;
        }

    }

private:
    T* _array;
    int _capacity;
    int _size;
};

//缺省参数不能给在函数定义,只能在声明给缺省值
//模板的作用域就是专门给一个函数或者一个类用的
//可以看{}的作用范围是那里,模板的作用范围就是哪里。
template<typename T>
Stack<T>::Stack(size_t capacity)
{
    
    
    _array = new T[capacity];
    _capacity = capacity;
    _size = 0;
}

template<typename T>
void Stack<T>::Push(const T& data)
{
    
    
    // CheckCapacity();
    _array[_size] = data;
    _size++;
}

When a function is defined outside a class, it needs to add the parameter list of the class template.

Note: It is not recommended to separate the declaration and definition of functions instantiated from templates.
Note: It is not recommended to separate the declaration and definition of functions instantiated from templates.
Note: It is not recommended to separate the declaration and definition of functions instantiated from templates.

The above example is just for demonstration.


Summarize

This article addresses issues related to function templates and class templates.

Guess you like

Origin blog.csdn.net/w2915w/article/details/130917798