In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to carry out C++ template explicit concrete analysis, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Although the template function is very easy to use, there are some problems. For example, some operations are not applicable to all types. C++ provides a solution for this situation, that is, to provide specific template definitions for specific types. The concrete here can be understood as the concrete of the type.
Let's take a look at an example in C++ Primer, and suppose we now have a structure called job:
Struct job {string name; double salary; int floor;}
The structure can be assigned as a whole, so the previous Swap function applies to it as well.
Template void Swap (T & a, T & b) {T temp = a; a = b; b = temp;}
But now we want to swap the structure with just the salary and floor fields, leaving the name unchanged. Because we want to introduce logic changes, it is not feasible to call the Swap function directly.
Of course, we can overload the function directly without the function template:
Void Swap (job & a, job & b) {/ / swap is the exchange function that comes with std. In the algorithm header file, swap (a.salary, b.salary); swap (a.floor, b.floor);}
Because C++ stipulates that non-function templates take precedence over function templates, we will give priority to this when calling the Swap function on the job structure.
In addition, we can provide a concrete template function:
Template void Swap (job & a, job & b) {swap (a.salary, b.salary); swap (a.floor, b.floor);}
The way this function is written looks a little special. We added template before the function type and followed by the function name. What it means is that this is an explicit embodiment of a function template, and it can also be understood that the previous function template provides a version of the job type. C++ stipulates that explicit template functions take precedence over normal template functions.
After reading the above, have you mastered the method of explicit and concrete analysis of C++ template? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.