Special and partial features of templates
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 implementationCode...
};
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
widget mycontroller> when defining objects, the compiler uses the aforementioned 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 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-specific mechanism. the following widget class template is the partial definition of widgets:
template // The original general definition is still used;
class widget // mycontroller is a specific class and is a special definition.
{< br>... 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 the Program , the compiler will try to find the most matched 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 not only use any
window with a specific mycontroller to customize widgets, you can also use any button with a specific mycontroller for special features
Widget:
template
class widget , mycontroller> // use any button to match the specific class mycontorroller
{< br>... code implementation with special features...
};
the template features powerful features. when you instantiate a template, the compiler compares the existing partial and full special templates, find 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 features/all featuresEach template parameter is a specific type to implement this template, andThe angle brackets behind the template do not contain any content.;
The template features a specific type only for some template parameters;