Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of C++ template reloading

Shulou Source: shulou.com Published: 2022-06-02 06:14:08 10月01日 Update

This article shows you an example analysis of C++ template overloading, which is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Overload template

Function templates make it very convenient for the same function to be used for different types. But sometimes the type is different, just through the template is not solved, may be logically different, this time just using the template is not solved.

To meet this requirement, we can overload the template like an overloaded function. Like regular functions, the functional characteristics of overloaded templates, that is, the number and type of input parameters must be different.

For example: for example, we defined a function template to exchange the values of two variables. If we exchange not just variables, but two arrays, we have to modify the logic.

Template void Swap (T & a, T & b); template void Swap (T * a, T * b, int n)

You can see that we passed in an extra int n, which represents the length of the array. In addition, the type of our input parameter has also changed, and it is no longer a reference to template type T, but a pointer. Because what we want to receive is an array, and the array is in the form of a pointer in the transfer of the function. So it's going to be written as a pointer here, and of course it can be written like this: t a [], there is essentially no difference between the two forms.

So if we realize it, it will be like this:

Template void Swap (T & a, T & b) {T temp = a; a = b; b = temp;} template void Swap (T * a, T * b, int n) {for (int I = 0; I)

< n; i++) { Swap(a[i], b[i]); }}2.问题 到这里,相信大家也能看出一点问题。 假设我们有这样一个模板函数: template void Swap(T a, T b); 虽然理论上类型T是万能类型,什么类型都可以接受。但我们操作的时候会有很多问题,比如我们执行a = b,对于数组类型就会报错。 再比如我们执行a >

B, many types cannot be compared in size. For example, doing arithmetic operations and so on, many types such as pointers, arrays or structures can not do arithmetic operations.

In short, the function of the template is very limited, sometimes can only deal with certain types, it is difficult to cover all cases. Of course, sometimes there are other ways to get around it, for example, structures can also overload comparison operators, you can also overload some arithmetic operators, and so on.

The above is the example analysis of C++ template overloading. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Templates types functions arrays time pointers operations different methods just arithmetic problems examples examples analysis two content variables forms skills Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Redmi MariaDB Huawei