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

Introduction and usage of C++ template function overloading

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "introduction and usage of C++ template function overloading". Many people will encounter this dilemma in the operation of actual cases. then 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!

1: if the instantiated template function is the same as the call of a non-template function, it will call the non-template function. However, you can also specify to call template functions, such as:

Inline int const& max (int const& a, int const& b)

{

/ / for ease of distinction, let the returned result be + 100

Return a < b? Axiom 10: baked 100

}

Template

Inline T const& max (T const& a, T const& b)

{

Return a < b? B: a

}

/ / here the instantiated form of the template function is the same as that of a non-template function.

You can use the following method to specify that the template function is called

Int I = max (42,66)

2: because the overloaded parameter of C++ template function is type, it does not support type conversion. However, non-template functions support type conversion:

Inline int const& max (int const& a, int const& b) {return a < b? B: a;} template inline T const& max (T const& a, T const& b) {return a < b? B: a;} / / since the templated function does not support type conversion, the non-templated max function max ('censor, 42.2) will be called here

Imagine that in the above code, if max ('censor, 42. 2) wants to call the template's max function, it must satisfy the condition that both arguments and return values are of the same type. However, the given two parameter types are inconsistent, and the template function does not support type conversion. Therefore, it cannot find a matching template function and will call a non-template max function. There will be a compilation error if we force the template function to be called using max ('clocked, 42.2).

This is the end of the introduction and usage of C++ template function overloading. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 217

*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