Template_12 Special and heavy load

Source: Internet
Author: User

1, overloaded function templates

F<int*> ((int*) pi);//1
F<int> ((int*) pi);//2
Replace t of the first template with int*, and int to replace t of the second template. You will then get two functions of the same name with the same parameter type (int*).
That is, not only can the template of the same name exist at the same time, but they each have an instantiated body of the same parameter type and return type.

f (pi); for both template argument deduction, F<int*> (int*) and f<int> (int*) can be successful. This means that the invocation is two semantic.
However, consider the additional rules for overload resolution: Select "Generate functions from more specific templates." Therefore, a second template is selected.
of course, if you want to choose between two special levels of the same template, you will not be able to make a decision which is to produce two semantics.

Template functions can also be overloaded with non-template function colleagues, and will prefer non-template functions.

2, Signature





3, explicit type of special
Class templates cannot be overloaded, but you can use another alternative mechanism to implement this transparent custom template feature, which is explicit specificity, also known as global specificity.
It provides the template with an implementation that enables template parameters to be replaced globally.
In fact, both the template and function templates can be replaced globally, and the members (member functions, embedded classes, static member variables, etc.) of class templates can be replaced globally.
Neither global or local specificity introduces a completely new template or template instance that simply provides another definition of an instance that has been implicitly declared in the original generic (or nonspecific) template .

Template<> class s< Special template arguments >

The implementation of global specificity does not require any association with generic implementations, and can contain member functions of different names. Global specificity is only associated with the name of the class template.

4, Global class template special






global template specificity and instantiated versions generated by templates cannot coexist in the same process In the sequence. When the


Instantiation is placed in the global template-specific location, and all template specificity is defined, this instantiation is actually a global template that is going to be special so OK.

If the global template-specific compilation unit is not in the same compilation unit as an instantiated or generic declaration, it is prone to errors that are difficult to catch.
We need to confirm that the special declaration is visible to the generic template so the user is.
Special declarations should usually be placed after the template declaration.
However, a generic implementation may also come from an external resource bundle, and we can create a header file that contains a generic declaration and let the specificity of what is behind it. The
should generally avoid having template specificity from external resource bundles.


5, global function template Special
Global function template specificity and class template specificity are broadly consistent, the only difference being that the function template specificity introduces the two concepts of overloading and argument deduction.



Special declarations prohibit the instantiation of templates;
to avoid duplicate definition errors, you cannot put the definition in a header file. The solution is to declare the specificity as an inline function, at which point the definition of the function can be placed in the header file.


6, Global member localization




7 , local class template special


Template_12 and overloading

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.