STL Template _ Concept

Source: Internet
Author: User

Templates and STL
I. Background knowledge of the template
1. Define different function versions for different types.
2. Use parametric macros to get rid of type limitations, and also because of lost type checking
into the risk.
3. With the help of the compiler preprocessor, according to the function macro framework, extended to different types of
Specific function version, on the one hand to write common algorithms and structures, on the other hand
Also has type security for function calls.
4. Generate specific functions from the compiler based on the function template--type parameterization.
int add (int x, int y) {...}//x and Y are value parameters
int a = ten, B = +, C;
c = Add (A, b);
Template<typename t>//T is a type parameter
T Max (t X, t y) {...}
Max<int> (...);
Max<double> (...);
Templates are more about using pre-defined templates: string/auto_ptr/list/qlist
Second, function template
1. A function template represents a family of functions, although it looks like a normal function, but its
A parameterized type (type parameter). When the compiler compiles a function template, the class
Type parameter programming specific types, generating the binary code of the specific function.
2. Definition of function templates
template< template Parameter Table >
return type template function name (call parameter table) {Template function body;}
Template parameter table: typename template parameter 1, typename template parameter 2, ...
The naming convention for template parameters is the same as the name of a generally valid identifier: in letters or under
The beginning of the dash, consisting of letters, numbers, or underscores.
The TypeName keyword can also be replaced with class, but the class here is not a table
class, and do not change to a struct.
Template<typename t>
T Max (t X, t y) {...}
Template<class t>
T Max (t X, t y) {...}
Max<student> (...);
Max<string> (...);
Max<int> (...);
Max<double> (...);
3. Use of Function templates
function template name < template parameter table > (call parameter table);
1) The compiler, based on the template arguments that are provided when the function template is called,
The process of compiling a number template into a specific function, called the instantiation of a function template.
2) The type used to instantiate the function template must satisfy the template's internal
For
3) Two compilations of function templates. The compiler sees the definition of the function for the first time because it does not
Knowing what type of template parameter it is, it cannot be generated as a binary instruction,
However, the compiler will first do a type-independent syntax check if there is no syntax error
, the compiler generates an internal representation based on the definition of the function template. When the compiler looks at
To a call statement to a function template, the specific template arguments provided are combined with the previous
Generates an internal representation of a binary instruction that produces a specific function.
4) for a function template be sure to let the compiler see the call statement at the same time, you can also see the template definition code, or the compiler can not complete the second compilation, there is no
method to generate a specific function code. The simplest way to do this is to put the definition of the function template in the header file, and all the source files that need to use the function template will contain
This header file.
4. Implicit inference of function template parameters
5. Overloading of function templates: While providing pass rules, specifically for certain special
The type gives a special implementation.
Three, class template
1. The return of the member variable, member function (normal/static/construct/destructor/operator) of the class
The return value/parameter/local variable, and the type in the base class can be parameterized, so that the
A template is called a class template.
2. Defining class Templates
template< template Parameter Table >
Class template name {...};
Each member function of a class template is a function template, regardless of whether it has a type parameter. If you define member functions outside the class template, you must follow the syntax of the function template
Define them.
tempalte< template Parameter Table >
return type class template name < template parameter table;:: member function name (call parameter table) {...}
3. Using Class templates
A class template is not a type, only if the remaining specific template parameters are combined
becomes a class, which is a concrete type. The class template becomes the process of a concrete class, called
The instantiation of the class template.
Class template name < template parameter table >
~~~~~~~~~~~~~~
class, access its static members, define variables/create objects
Class templates can only be instantiated explicitly, and implicit inference is not supported.
Compile-time run-time
Class template-instantiation--instantiation--Object
Compiler processor
Template parameters for class templates can have default values, and parameter defaults must be on the right.
The function template in c++98 is not available with a default value, but the function in c++2011
Templates can also have default values (-std=c++0x).
4. Instantiation of class templates
1) In a class template, only those member functions that are called are instantiated, that is, the resulting
Binary code.
2) Although some types do not provide all the functionality required for a class template, they can still be
To instantiate the class template, as long as it does not directly or indirectly invoke those dependent on the non-supplied
Can be a member function.
5. Static member variables for class templates, in each instantiation class of the class template, have
A separate copy and is shared for all instantiated objects of the instantiated class.
6. Recursive instantiation of class templates
Instantiate the class template itself with the instantiated type of a class template. In this way, you can
It is easy to construct a logical model with recursive structure in space.
list<array<int> >: Array linked list
array<list<int> >: array of linked lists
list<list<int> >: Two-dimensional linked list
array<array<int> >: two-dimensional array
7. The specificity of class templates
1) through a special class template, you can optimize for a specific type of implementation, or the gram
The lack of a specific type when instantiating a class template.
2) The Special class template requires that all member functions of the class template be special-all-class-specific.
Template<>
Class template name < specificity for template parameters > {...};
The full-class version can be completely different from the basic version.
3) If the class template is only part of the implementation of the member function and type-dependent, then you can also
Special for this member function only-member specificity.
The special version of a class template member function shares the same declaration as its base version, so
The functional prototypes of the special version and the basic version must be strictly consistent in addition to the template parameters.
8. Local localization of class templates (partial specificity)
1) class templates can be localized, that is, specifying a specific implementation for a class template, and another
On the one hand, it allows the user to specify the partial template parameters themselves.
2) If multiple local specificity matches a claim to the same extent, the declaration will be
Ambiguity leads to ambiguous errors.
3) Function templates do not support local specificity.

STL Template _ Concept

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.