The precedence of C + + common functions and template functions and the overloading of special functions

Source: Internet
Author: User

In the face of C + + template, need to pay attention to, because the template of the complexity of a lot of circumstances, so the best way to learn the template I personally think that is used to learn, do not use to see all kinds of odd door strange skills, because you even see, very difficult to understand the template to achieve internal, including meta-programming AH what, But I believe very little in the work, and soon you will forget it, so, for the template, we can take the idea of where to learn, which can save time and maximize efficiency.

Today is mainly about the problem of template specificity already overloaded with functions, simply cite the example

1 void say (int<typename t>  void  Say (T value); 3 Template< >  void say (int value);

As with the three functions, the C + + default rule, if there is a non-template function to do an exact match (including Const and Const &) will first match the non-template function, then match the special template, and then match the template, so the above order is if I call say (1) for 1 > 3 > 2. Note that it is critical to allow your normal functions to be used in a matching process without the need for a so-called "exact Match", which can have some appropriate changes such as const or const &.

Here I quoted Baidu Encyclopedia on the introduction of overloading to deepen the image impression:

In overloading and function template overloading, the compiler chooses a function that takes the following three steps, which is called overload resolution. The first step: Create a list of candidate functions with the same function and template functions as the name of the called function. Step Two: Create a list of viable functions using the list of candidate functions. These are all functions with the correct number of arguments. Step three: Determine if there is a best possible function. If there is one, use it. Determine the best function, considering only its signature, regardless of the return type (it is not considered, but if it is hard to find a way, there is no need to waste resources for unnecessary performance). To determine the best function, the matching feature is judged by the following: (1) exact match (normal function is better than template; allow insignificant conversions) (2) promote matching (such as char and short auto Convert to int) (3) standard conversion (int to char, Long to Double) (4) A user-defined conversion (such as a conversion function defined in a class declaration). Completely allow insignificant conversions, including references, between pointers and entities, between arrays and pointers, between functions and function pointers, const and non-const, and so on.

Finally, attach a more easily mixed demo

#include <iostream>using namespacestd; template<classT>voidFunc (T &1) {cout<<"Template version!"<<Endl; cout<<sizeof(s) <<": \ t"<< s <<Endl;} voidFuncConst Char*s) {cout<<"Special version!"<<Endl; cout<<sizeof(s) <<": \ t"<< s <<Endl;} intMain () {CharS[] ="ASDASDASD";    Func (s); return 0;}

This demo, call the time will go to call Templation version (Songxiawuren remind the CL under Windows is the default is into special, I use GCC). Incidentally, an excerpt from Daniel's comments:

The reason is that S has experienced an identity conversion for the template function, rank is exact Match, the argument is an array type, and the parameter is a pointer type, so there are array-to-pointer Conversion and qualification conversions, the rank of the two, the final conversion rank is exact Match. However, only rank, is the same, no difference. However, in the same case, the identity conversion is better than the non-indentity conversion, so the reference binding is better than the latter.

So we can try to remove the const, because without the qualification conversions, this time will be the first to call special version.

Reference Link: Http://baike.baidu.com/view/126530.htm?fr=aladdin

http://bbs.csdn.net/topics/390577993

The precedence of C + + common functions and template functions and the overloading of special functions

Related Article

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.