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

Detailed explanation of C++ display materialization and display instantiation

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "detailed explanation of C++ display materialization and display instantiation". Many people will encounter such a dilemma in the operation of actual cases. next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Today, we see C++ primer plus Chapter 8-function Exploration, in which the template function section has two concepts that are easy to get confused: record here:

First look at the definition of template function: I think it is mainly to save the amount of code, different types of completion of the same operation, if you use function overload, for the basic data type shaping, there are short/int/long/longlong 4, not including unsigned and signed, so you need a large number of overloaded functions, and the content is the same, a waste of copying and pasting operations. Therefore, the template function is used to realize it.

1. Declare the template by placing it at the beginning of the function using template or template. Typename is the new standard and class is for forward compatibility. Therefore, writing class is more common.

2, note that templates can not shorten the length of the executable file, when the function is called, the compiler must generate instantiated template functions (called implicit instantiation instance) according to the given parameter type, namely int add (int a, int b); so the template function just gives the programmer what to do to the compiler.

3. Templates can be overloaded, as shown in template 1, template 2 and template 3 above. Note that overloading is used to change the type of input parameters or to increase or decrease the number of input parameters (the latter is generally realized by default parameters! ).

We enter add (1Magne2) to get 3, and display to call template 1; enter add (1Magne2 to 3) to get 6, call template 2; enter add to get 'Bread, call template 3; pay attention to the writing of multiple types in template 3.

4. the display of the template is specific (key point)

For example, for template 2, we cannot use it to add int* types, so we need the specialize template, that is, to specialize (instantiate) the input of the int* type, so that it can have different functions from the original template. Note that the display of materialization can not only be declared, there must be a function body, otherwise vs will report a link error!

My understanding is: to show the application range of the template broadened by materialization! Originally, the add function can only be used for types that overload the "+ operator", but now it can also be used for int arrays, but in fact, you can write another function add_array to achieve this function. It feels like it is for good-looking (in line with a certain unified style), so write it together!

5. Explicit instantiation of template

The explicit instantiation of the template is really just a declaration: template int add (int,int); note that it is not after template, but it is best to add it after add, that is, the type you want to instantiate.

In fact, I am not very clear about the role of explicit instantiation, because if you modify the function body, you can use materialization, instantiation can not take the function body, so referring to http://bbs.csdn.net/topics/390559301, you can probably get a conclusion:

Cout

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

Internet Technology

Wechat

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

12
Report