Templates in C ++ are classified into class templates and function templates,. Special Template
(1) Special Class Template
Sometimes for the sake of need, for specific types, the template needs to be made special, that is, special processing. for example, a stack template is applicable to the bool type. In fact, the bool type requires only one binary bit to store it. Using one word or one byte is a waste of storage space.
Template <class T>
Class Stack {};
Template <>
Class Stack <bool> {//... //};
In the preceding definition, template <> tells the compiler that this is a special template. Make sure that you have a non-specific declaration before declaring a special template! And the names of the two classes are the same!
(2) Special Function Template
See the following example.
Main ()
{
Int highest = mymax (5, 10 );
Char c = mymax ('A', 'z ');
Const char * P1 = "hello ";
Const char * P2 = "world ";
Const char * P = mymax (P1, P2 );
}
The first two mymax can return the correct results, but the third one is not, because mymax directly compares the two pointers P1 and P2 instead of the content to which it points.
In this case, when the parameter type of the mymax function is const char *, it must be customized.
Template <class T>
T mymax (const t T1, const t T2)
{
Return t1 <t2? T2: T1;
}
Template <>
Const char * mymax (const char * T1, const char * t2)
{
Return (strcmp (T1, T2) <0 )? T2: T1;
}
Now mymax (P1, P2) can return correct results.
4.Biased templates
The template's special feature refers to the need to implement special features based on some but not all parameters of the template.
(1) bitrate of class templates
For example, the definition of Vector class in the C ++ standard library
Template <class T, class Allocator>
Class vector {//... //};
Template <class Allocator>
Class vector <bool, Allocator> {//... //};
In this case, one parameter is bound to the bool type, and the other parameter is not bound to the bool type, which must be specified by the user.
(2) features of function templates
Strictly speaking, function templates do not support bitwise features. However, functions can be overloaded to achieve bitwise effects similar to class templates.
Template <class T> void F (t); ()
Reload (a) according to the overload rule
Template <class T> void F (t *); (B)
If (a) is called the base template, (B) is called the overload of the base template (A), rather than the bitwise of (. The C ++ Standards Committee is still discussing whether to allow function template skewness in the next version.
5.Matching rules for template Specialization
(1) matching rules of class templates
Optimized is superior to secondary-specific, that is, the most precise matching of template parameters has the highest priority.
Example:
Template <class T> class vector {//... //}; // (A) Common
Template <class T> class vector <t *> {//... //}; // (B) Special pointer type
Template <> class vector <void *> {//... //}; // (C) Special void *
Each type can be used as a common (a) parameter, but only the pointer type can be used as a (B) parameter, and only void * can be used as a (c) parameter.
(2) matching rules of function templates
Non-template functions have the highest priority. If no matching non-template function exists, the most matched and exclusive functions have a high priority.
Example:
Template <class T> void F (t); // (d)
Template <class T> void F (INT, T, double); // (E)
Template <class T> void F (t *); // (f)
Template <> void F <int> (INT); // (g)
Void F (double); // (h)
Bool B;
Int I;
Double D;
F (B); // call with T = bool (d)
F (I, 42, d) // call with T = int (E)
F (& I); // call with T = int * (f)
F (d); // call (h)