Special and partial features of templates

Source: Internet
Author: User
Special and partial features of templates

The reason why the template needs to be made special is that the compiler believes that if you can better implement a function for a specific type, you should listen to it.

Templates are classified into class templates and function templates. features are classified into full and partial features. All features are specific types of the implementation of the dead template. If the template has multiple types, only some of them are limited.


Partial template specialization allows you to define a subset in all possible entities of a template.
1.Template specialization ):
For example, the following template is defined:
Template <class window, class controller>
Class widget
{
... Generalized implementation code...
};
Then you can make it as explicit as below:
Template <> // Note: The angle brackets behind the template do not contain any content;
Class widget <modaldialog, mycontroller>
{
... Special implementation code...
};
Modaldialog and mycontroller are other classes defined by you. With the special definition of this widget, if you define a widget <modaldialog, mycontroller> object in the future, the compiler uses the preceding special definition. If other generic objects are defined, the compiler uses the original generalized definition. This is the template's special definition.

2.Partial template specialization (the template is special)
Template specialization is implemented by "giving all template parameters in the template a specific class. the template features are implemented by "giving some template parameters in the template specific classes, while leaving the remaining template parameters still using the original generalized definition;
For example, for the definition of the widget class template above, sometimes you want to use a specific mycontroller class special widget for any window. In this case, you need to use the template special mechanism. the following widget template is the partial definition of widgets:
Template <class WINDOW> // still use the original generalized definition;
Class widget <window, mycontroller> // mycontroller is a specific class, which is a special definition;
{
... Code implementation with special features...
};
This is a special definition. A mycontroller class can be used with any window.
In the partial special definition of a class template, you only need to specify some template parameters and leave other generalized parameters. when you implement the above class template in a program, the compiler will try to find the most matching template definition. this search process is very complex and fine-grained, allowing you to differentiate in creative ways. for example, if you have a button template with a template parameter, you can use any window with a specific mycontroller to customize the widget, you can also use any button with a specific mycontroller to optimize the Widget:
Template <class buttonarg>
Class widget <button <buttonarg>, mycontroller> // use any button to match the specific class mycontorller
{
... Code implementation with special features...
};
Templates are highly specialized. when you instantiate a template, the compiler compares the existing partial and full special templates and finds out the most appropriate and matched implementations. in this way, the flexibility is great. however, unfortunately, the bitrate mechanism of the template cannot be used in functions, whether it is a member function or a non-member function.

Note:
1. Although you can fully specialize in the member functions in the class template, you cannot be partial to them;
2. you cannot describe the namespace-level functions (non-member ). function Overloading is the most specific mechanism close to "namespace-level template functions", which means that you have "function parameters" (rather than the return value type or internal use type) excellent special ability;
3. Special or full special, NO content is included in the angle brackets behind the template;

Summary:
Template special/full special refers to a specific type for each template parameter to implement this template, and no content is included in the angle brackets behind the template;
The template features a specific type only for some template parameters;

Special and partial features of templates

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.