Special and special

Source: Internet
Author: User

Abstract: This article introduces the template features and features widely used in the C ++ standard library through examples, and points out the template-specific and biased definition rules and application rules.
Keywords: Template, special, partial special
1. Introduction
The templates in C ++ are divided into class templates and function templates. Although they have not been introduced to the C ++ standard for a long time, they have been widely used, this is fully reflected in STL. At present, STL has gained wide attention, application and research in the C ++ community. Understanding and understanding templates is the basis for learning, applying and researching, and expanding STL. STL template instances are filled with a large number of special and biased templates.
2. template Definition
(1) class template
Defines a stack class template, which can be used to accommodate different data types
Description:
Template <class T>
Class Stack {
PRIVATE:
List * top;
Public:
Stack ();
Stack (const stack &);
~ Stack ();
Void push (T &);
T & POP ();
//...
};
In the preceding definition, the template tells the compiler that this is a template. The <class T> parameter in the angle brackets specifies one or more template parameters. You can specify one or more parameters for implementation, the keyword class in template <class T> can be replaced by the keyword typename.
Except for specifying the template parameters when declaring a class template, the other classes are the same as common 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 we want to define a max function to return the setter 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 that in the class template definition.
The use of template functions is the same as that of common non-template functions, because the parameters of template functions can be parsed from the input parameters. For example:
Int highest = mymax (5, 10 );
Char c = mymax ('A', 'z ');
Mymax (5, 10) parses the template function parameter as int, mymax ('A', 'z') parses the template function parameter as char.
3. 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.
(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. Special Template
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 (g)

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.