In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the call of the general formula of function templates in C++". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "the call of the general formula of function templates in C++" together.
Template is a tool that supports parameterization polymorphism in C++. Using template, users can declare a general pattern for a class or function, so that some data members in the class or parameters and return values of member functions can obtain arbitrary types.
A template is a tool for parameterizing types;
There are usually two forms: function templates and class templates;
Function templates are for functions that differ only in parameter types;
Class templates are for classes that differ only in data member and member function types.
The purpose of templates is to enable programmers to write type-independent code. For example, if you write a swap function that swaps two int types, this function can only implement int types. For double, characters, these types cannot be implemented. To implement these types of swaps, you have to rewrite another swap function. The purpose of using templates is to make the implementation of this program independent of type, such as a swap template function, which can implement int type and double type exchange. Templates can be applied to functions and classes. They are described separately below.
Note: Declarations or definitions of templates can only be made at global, namespace, or class scope. That is, it cannot be done within a local scope function, such as declaring or defining a template in the main function.
Function template general formula
Format of function template:
template return type function name (parameter list)
{
function body
}
Template and class are related words, class can be replaced by typename related words, here typename and class are no different, the parameters in parentheses are called template parameters, template parameters and function parameters are very similar, template parameters cannot be empty. Once a template function is declared, it is possible to declare member variables and member functions in a class using the parameter name of the template function, i.e., template parameter names can be used wherever built-in types can be used in the function. Template parameters require template arguments supplied when the template function is called to initialize template parameters, and once the compiler determines the actual template argument type, it is said to instantiate an instance of the function template. For example, the template function for swap is of the form:
templatevoidswap(T&a,T&b){},
When such a template function is called, the type T is replaced by the type at which it is called, such as swap(a,b) where a and b are int, then the parameter T in the template function swap is replaced by int, and the template function becomes swap(int&a,int&b). When swap(c,d) where c and d are double types, the template function is replaced with swap(double&a,double&b), thus implementing type-independent code for the implementation of the function.
Note: For function templates, there is no call such as h(int,int). You cannot specify the type of template parameter in the parameter of function call. Calls to function templates should be made using argument deduction, that is, only calls such as h(2,3) or inta,b;h(a,b) can be made.
Thank you for reading, the above is the "C++ function template general formula call" content, after the study of this article, I believe we have a deeper understanding of the C++ function template general formula call this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.