Go The special and partial localization of templates in C + +

Source: Internet
Author: User

Reprinted from: http://hi.baidu.com/klcdyx2008/blog/item/5adbf77b79f316f90bd1873c.html

1. Introduction
Templates in C + + are classified into class templates and function templates, although the time it introduces to the C + + standard is not very long, but it has been widely used, which is fully reflected in the STL. At present, STL has been widely concerned, applied and researched in C + + community. Understanding and mastering templates is the basis for learning, applying, and researching and expanding STL. In the case of STL template, there is a lot of template special and partial special.


2. Definition of templates
(1) class template

Defines a stack of class templates that can be used to accommodate different data types
The description is as follows:
Template <class t>
Class Stack {
Private
list* top;
Public
Stack ();
Stack (const stack&);
~stack ();
void push (t&);
t& pop ();
...
};
In the definition above, the template tells the compiler that this is a template, the angle brackets in the <class T > indicates the parameters of the template, can have one or more, specifically implemented when specified by the user, where template <class T > The keyword class can be replaced with the keyword typename.
The use of class templates, in addition to specifying template parameters at the time of declaration, is the same as for ordinary classes, for example:
Stack<int> Int_stack;
Stack<char> Ch_stack;
Stack<string> Str_stack;
Int_stack.push (10);
Ch_stack.push (' z ');
Str_stack.push ("C + +");
(2) function template
Suppose you now want to define a max function to return the largest of two values of the same type (this type is allowed to be compared).
Template<class t>
T Mymax (const t& t1,const t& T2)
{return T1 < T2 t2:t1;}
The meaning of template <class t> is the same as in the class template definition.
The use of template functions is the same as for normal non-template functions, because the parameters of a template function can be parsed from its incoming parameters. For example:
int highest = Mymax (5,10);
char c = Mymax (' A ', ' Z ');
Mymax (5,10) resolves the template function parameter to int, Mymax (' A ', ' z ') resolves the parameter of the template function to char.


3. Template-Specific
(1) class template special

Sometimes for the sake of a specific type, the template needs to be specialized, that is, special processing. For example, the Stack class template is for bool types, because the bool type can be stored with a single word or a byte, because it is actually a bits.
Template <class t>
Class Stack {};
Template < >
Class Stack<bool> {//...//};
In the definition above, template < > tells the compiler that this is a special templates.
(2) The specificity of function template
Look at 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 result. But the third cannot, because, at this time Mymax directly compares two pointers P1 and p2 instead of what they point to.
In this case, when the parameter type of the Mymax function is const char*, it needs to be special.
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 to the correct result.


4. template's Partial localization
Template biasing refers to the need to be specific to some but not all of the parameters of the template
(1) The partial specificity of the class template
For example, the definition of a class vector in the C + + standard library
Template <class T, class allocator>
Class Vector {//...//};
Template <class allocator>
Class Vector<bool, allocator> {//...//};
In this biased example, one parameter is bound to the bool type, and the other parameter is still unbound and needs to be specified by the user.
(2) The partial specificity of function template
Strictly speaking, function templates do not support biasing, but because functions can be overloaded, it is possible to achieve a similar effect to class template biasing.
Template <class t> void f (T); (a)
Overloading of (a) based on overloading rules
template < class t> void F (t*); (b)
if (a) is called a base template, then (b) is called an overload of the base template (a) rather than a bias to (a). The standard Committee for C + + is still discussing whether to allow the partial localization of function templates in the next release.


5. Matching rules when template is special
(1) Matching rules for class templates

Optimization is better than sub-special, that is, template parameters most accurately match the highest priority
Example:
Template <class t> class vector{//...//}; (a) General type
Template <class t> class vector<t*>{//...//}; (b) Specificity of the pointer type
Template <> class vector <void*>{//...//}; (c) void* of the Special
Each type can be used as an argument to the normal type (a), but only the pointer type can be used as a parameter of (b), and only void* can be a parameter of (c)
(2) matching rules for function templates
Non-template functions have the highest precedence. If there are no matching non-template functions, then the most matching and most specific functions have 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); Called with T = bool (d)
F (i,42,d)//called with T = Int (e)
f (&i); Called with T = int* (f)
F (d); Call (g)

Reprinted from: Http://blog.csdn.net/zhang810413/archive/2007/12/18/1948603.aspx

Add:

The Partial template specialization enables you to create a subset of all possible entities in a template.
1.Template Specialization:
For example, define one of the following templates:
Template<class Window, Class controller>
Class Widget
{
... Generalization implementation Code ...
};
Then you can make it explicit as follows:
Template<>//Note: There is no content in the angle brackets behind the template;
Class Widget<modaldialog, mycontroller>
{
... Special implementation Code ...
};
Where Modaldialog and Mycontroller are other classes you define yourself, and with this widget's special definition, if you later define Widget<modaldialog, mycontroller> objects, The compiler uses the above-mentioned specialization definition, and if other generic objects are defined, the compiler uses the original generalization definition, which is the template specialization.

2.Partial Template Specialization (templating bias)
Template specialization is achieved by "giving a specific class to all template parameters in the template." The template bias is achieved by "giving the template parameters in a specific class, leaving the remaining template parameters in the same way as the original generalization definition".
For example, for the definition of the Widget class template above, sometimes you want to match a particular Mycontroller class widget to any window. This is the time to use the template bias mechanism. The following Widget class template is the widget's partial definition:
Template<class window>//The original generalization definition is still used;
Class Widget<window, mycontroller>//mycontroller are specific classes, is the definition of specificity;
{
... Partial implementation of code ...
};
This is a partial definition; a Mycontroller class can be paired with any of the windows.
Typically in a generic definition of a class template, you will only specify certain template parameters and leave other generalization parameters. When you implement the above-mentioned class templates in your program, the compiler will try to find the most matching template definitions. The search process is very complex and sophisticated, allowing you to be creative in the way of specialization. For example, Suppose you have a button class template that has a template parameter, then you can use any of the window with a specific mycontroller to special widgets, you can also take any button with a specific mycontroller to the special widget:
Template<class buttonarg>
Class Widget<button<buttonarg>, mycontroller>//Use any Button with specific classes Mycontorller
{
... Partial implementation of code ...
};
The template's ability to be biased is powerful. When you instantiate a template, the compiler compares the existing biased template with the full-specificity template, and finds the most appropriate and matching implementation. In this way, flexibility is great. But unfortunately, the template's biasing mechanism cannot be used on functions, Whether a member function or a non-member function.

Attention:
1. Although you can fully special member functions in the class template, you cannot bias them;
2. You cannot override the namespace-level (Namespace-level) function (Non-member). The closest approximation to the namespace-level template function is the function overload, which means you have a "function argument" (not a return value type or an internal type) Have a very refined ability to special;
3. In the case of special or full specificity, there is no content in the angle brackets behind the template;

Summarize:
Template Special/mission-wide refers to each template parameter a specific type, in order to implement the template specifically, and the text behind the angle brackets without any content;
Template biasing refers to only a specific type of template parameter, to implement this template;

Go The special and partial localization of templates in C + +

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.