In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the template of C language and generic programming". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the template of C language and generic programming?"
Templates and generic programming Abstracts (Effective C++):
The initial motivation for C++template was straightforward: it allowed us to build "type-safe" containers such as vector,list and map. However, as more people use templates, they find that template has the ability to make more possible changes. Containers are fine, of course, but generic programming (generic programming)-- the code written is independent of the types of objects it handles-- is even better. STL algorithms such as for_each,find and merge are the result of this kind of programming. It turns out that the C++template mechanism itself is a complete Turing machine: it can be used to calculate any computable value. So template metaprogramming (template mataprogramming) is exported to create a program that "executes within the C++ compiler and stops when the compilation is complete."
A brief introduction to template and generic programming
Both object-oriented programming (OOP) and generic programming can handle situations where the type is not known when the program is written; the difference between the two is that OOP can handle situations where the type is unknown before the program runs, while in generic programming, the type is known at compile time.
In our commonly used STL standard library, each container provides a single, generic definition, such as our commonly used vector, and we can define many types of vector.
Vector vi; / / vi is an instance of a vector container of type int vector vs; / / vs is an instance of a vector container of type string vector vd; / / vd is an instance of a vector container of type double
A template is the foundation of generic programming. A template is a blueprint or formula for creating a class or function.
Function template / / simple comparison function template templateint cmp (const T & v1 Magneto Const T & v2) {if (v1v2) return 1; else return 0;}
The function definition starts with the keyword template, followed by a list of template parameters, which is a comma-separated list of one or more template parameters, surrounded by angle brackets
* * Note: * * in the template definition, the template parameter list cannot be empty.
The template parameter list represents the type or value used in the class or function definition. When we use a template, we can (explicitly or implicitly) specify the template argument and bind it to the template parameter
A simple understanding of the instantiation process of a template
As we all know, when you think template programming is very smart, there must be something that carries the weight for you. C++ provides the ability of templates and generic programming. This means that there is something that dynamically implements the function of templates for you, and this must be something lower than the high-level language level of C++, and what we know is lower than the C++ high-level language, except the operating system. Is the compiler.
When we call a function template, the compiler (usually) uses function arguments to infer template arguments for us. To put it simply, when we call a function template, the compiler determines the type bound to the template parameter T by using the type of argument
Coutpush_back (std::move (val));} private: std::shared_ptr data; / / if data [I] is invalid, throw msg void check (size_type iMagne Const std::string & msg) const;}
Similar to function templates, class templates start with the keyword template, followed by a list of template parameters. In the definition of a class template (and its members), we use the template parameters as avatars instead of the types or values that the user needs to provide when using the template
* * Note: * * each instance of a class template forms a separate class, and each instance of a class template has its own version of the member function.
So, we may have a single template that does not meet all types of requirements, and template specialization occurs.
Instantiation of member function of class template
By default, the member function of a class template is instantiated only when the program uses it
/ / the constructor that instantiates T_vector and accepts initializer_list T_vector T_vi = {0Perry 1pm 2jue 3je 4je 5}
If a member function is not used, it will not be instantiated
Why do we need template specialization?
When we write a single template, it is most appropriate for any possible template argument and can be instantiated, but it is often too idealized. In some special cases, the definition of a universal template may not be appropriate for a particular type, and a universally defined template may fail to compile or be imperfect.
Therefore, when we cannot (or do not want) to use the template version, we can define a specialized version of the class or function template.
Define function template specialization
/ / the original special version of the cmp function, which is used to handle the pointer templateint cmp (const char* const& p1Magne Const char* const& p2) {return strcmp (p1zoc p2);} / / the original special version of the cmp function, used to handle the pointer templateint cmp (const char* const& p1Magne Const char* const& p2) {return strcmp (p1p2);}
The difference between function overloading and template specialization
When defining a specialized version of a function template, we essentially take over the work of the compiler. That is, we provide a definition for one of the special instances of the original template. In short, the essence of instantiation is to instantiate a template rather than overload it, so specialization does not affect function matching
Note:
To specialize a template, the declaration of the original template must be in the scope
Before any code that uses a template instance, the declaration of the specialized version must also be in the scope
The declaration of all templates with the same name should be placed first, followed by the specialized version of these templates
Partial specialization of class template
Unlike function templates, the specialization of class templates does not have to provide arguments for all template parameters. The partial specialization of a class template is itself a template, and when using it, the user must also provide arguments for the template parameters specified in the specialized version.
Note: we can only partially specialize the class template, not the function template
At this point, I believe you have a deeper understanding of "what is C language template and generic programming". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.