Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of C++ template reloading

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report