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

Post-example Analysis of template function and return Type of Category 11

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

Share

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

This article mainly introduces the "template function and return type post example analysis". In the daily operation, I believe that many people have doubts about the post example analysis of template function and return type. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "template function and return type post example analysis". Next, please follow the editor to study!

Progressive description

The simplest case

Consider that we have a function that takes one of the elements from an array of integers.

The code is simple, but it's just an introduction. All the sample code in this article does not take into account the situation where the subscript is out of bounds, which makes the topic more prominent.

Suitable for other types

If you want this function to adapt to more types of arrays, just introduce a template.

It's not that hard.

More general

It's not that easy if you want to apply data types to vector in addition to extensible data types. For example, the following code cannot be compiled.

Cannot be compiled because dereferencing is not a data type, but an operation. If the template variable is T and the return value is T*, it can be compiled normally.

The solution to this problem is to use the aforementioned new features of Category 11: return value type postpinning and decltype. The code is as follows:

Because decltype needs to get the type that it dereferenced, the operation to get the return value type must be placed after the emergence of it, the so-called return value type. With this template function, the code can be executed correctly in the following 3 cases:

Other situations.

This approach applies not only to dereferencing scenarios, but also to other situations where the return value type can be obtained through parameters. For example, if you get member x from the following DblPoint and IntPoint structures:

You can define a template function as follows:

At this point, the study of "post-example analysis of Cconfigured 11 template functions and return types" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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