Priority of C ++ common functions, template functions, and special function Overloading

Source: Internet
Author: User

Priority of C ++ common functions, template functions, and special function Overloading
In the face of the C ++ template, you need to pay attention to it because of the complexity of the template, there are many situations, so it is best to learn the template method. I personally think that you will learn it when using it, if you don't use it, try not to look at the various strange tricks, because even if you read it, it's hard to understand the implementation of the template internally, including the meta programming, but I believe it is rare to actually use the work, you will soon forget about the template, so we can take the concept of where to use the template to learn, which can save time and maximize efficiency. Today we will focus on the problem that the template has been converted into a function overload. For example, copy the Code 1 void say (int value); 2 template <typename T> void say (T value ); 3 template <> void say (int value); copy the code, such as the three functions. C ++ default stipulates that if non-template functions exist, they are fully matched (including const and const &) match non-template functions first, then match the special template, and then match the template. So the above Order is if I call say (1) to 1> 3> 2. Note: In the matching process, it is critical that your common functions do not need to perform the so-called "exact match". Some appropriate changes may occur, such as const or const. here I will refer to the introduction to overload in Baidu encyclopedia to deepen the image's impression: in overload and function template overloading, the compiler selects a function. This process is called overload resolution in the following three steps. Step 1: create a list of candidate functions, including functions with the same name as the called function and template functions. Step 2: Use the list of candidate functions to create a list of feasible functions. These are all functions with the correct number of parameters. Step 3: Determine whether there are the best feasible functions. If yes, use. To determine the optimal function, we only consider its feature mark, not the return type (or not). However, if we try to find a solution, but there is no need to waste resources for unnecessary performance ). Determine the optimal function. matching the feature mark must go through the following judgment in sequence: (1) Full matching (the conventional function is superior to the template; irrelevant conversions are allowed) (2) promote matching (such as automatic conversion of char and short to int) (3) standard conversion (conversion of int to char and long to double) (4) user-Defined conversions (such as the conversion functions defined in the class Declaration ). Allow irrelevant conversions, including references, between pointers and entities, between arrays and pointers, between functions and function pointers, const and non-const. Finally, we will attach a demo copy code that is easy to mix up # include <iostream> using namespace std; template <class T> void func (T & s) {cout <"template version! "<Endl; cout <sizeof (s) <": \ t "<s <endl;} void func (const char * s) {cout <"special version! "<Endl; cout <sizeof (s) <": \ t "<s <endl;} int main () {char s [] = "asdasdasd"; func (s); return 0 ;}copy the code demo, the version of templation will be called during the call (cl in Windows is included in special by default, and gcc is used here ). By the way, I would like to extract a comment from Daniel: the reason is that s experienced an identity conversion for the template function, its rank is Exact Match, while the real parameter is an array type for the common function, and the form parameter is a pointer type, so here there are array-to-pointer conversion and qualification conversions. The rank of the two is the same, and the final transformation rank is Exact Match. However, compared with rank only, they are all the same and there is no difference. However, in the same case, identity conversion is better than non-indentity conversion, so the referenced binding is better than the latter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.