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

What is the specific concept of C++ template parameters?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the specific concept of C++ template parameters, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

C++ programming language is a powerful computer application language, which greatly reduces the burden of developers and improves the development efficiency to a certain extent. Let's first take a look at the concepts of C++ template parameters. To put it simply, you can think of a template as a type, and a function template is no exception.

Since it is a type, we should use an instance of it when using a template function. Since it is the relationship between type and instance, there should be a problem of type instantiation. When we instantiate a common type, we usually need to provide the necessary parameters, and template functions are no exception. It's just that C++ template parameters are not ordinary parameters, but specific types. That is, when instantiating a function template, you need to take the type as an argument. In general, template parameters are divided into template parameters and call parameters. For example:

Template inline RT const& max (T1 const& a, T2 const& b) {/ / TODO: code implementation. }

The * line defines the function template parameters; the function parameters in the second line define the call parameters. It should be noted that the return value does not belong to the call parameters of the function template.

When calling a template, the most important thing is to correctly derive the C++ template parameters. Here are a few points to pay attention to:

1: the instantiated function template shown. For example:

Template inline T const& max (T const& a, T const& b) {return a < b? B: a;} / / instantiate and invoke a template max (4,4.2)

Line 10 instantiates a template by specifying that the C++ template parameter is double.

2: implicitly instantiate a function template. For example:

Template inline T const& max (T const& a, T const& b) {return a < b? B: a;} / / implicitly instantiate and call a function template int I = max (42,66)

Line 8, we do not show the specified function template parameter, but it can automatically deduce that the function template parameter is int. There may be a problem here. What will happen if the definition of a non-template function is the same as that of a derived template function instance? For example:

Inline int const& max (int const& a, int const& b) {/ / to make it easier to distinguish, return the result + 100 return a < b? A const& 10: baked 100;} template inline T const& max (T const& a, T const& b) {return a < b? B: a;} / / is this a template function or a non-template function? Int I = max (42,66)

In fact, the code in line 14 first goes back to see if there are any non-template functions that meet the requirements; if not, match and instantiate the corresponding template functions based on the parameters. Therefore, it should call a non-template max function.

3: you can also use some of the default C++ template parameters. You don't have to specify all the template parameters, for example, you can specify some of the parameters from left to right. For example:

/ / three parameters template inline RT const& max (T1 const& a, T2 const& b) {/ / TODO: code implementation are defined from left to right. } / / you can specify only * * return parameters. That is, the double type max (4,4.2) is required.

In the above code, since the return parameter type is not a call parameter, it must be explicitly specified as the double type. T1 and T2 belong to the template parameters of calling C++, which can be deduced from the function call.

On the C++ template parameters of the specific concept is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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