Chapter 1 about this book
1.4
For the use of const in template programming, we recommend that you use the int const instead of the const int style.
Chapter 2. function templates
2.1.2 using the template
The function template must be compiled twice:
Without instantiation: Check syntax error, for example, loss ';'.
Part 2 (at time of Instantiation): checks and confirms that all calls are valid
This leads to a problem when using the template: when instantiating a function template, the definition of this function template must be known to the compiler; this is different from the common compilation and connection differences for common functions-only the declaration of a function is sufficient for compilation and use.
2.2 argument deduction
Note: automatic conversion is not allowed during argument deduction.
2.3 template parameters
The function template has two types of parameter:
1. template parameter: "<...>" before the function Template Name
2. Call parameter: appears in "(...)" after the function Template Name
Unlike class templates, function templates cannot have default template argument. This is more of a legacy problem than a technical obstacle, in the future, the C ++ standard may be allowed.
Sometimes, programmer must specify the template argument explicitly because they cannot be determined through "function template argumetn deduction"
2.4 overloading function templates
It is also possible to specify explicitly an empty template argument list. This syntax indicates that only templates may resolve a call, but all the template parameters shoshould be deduced from the call arguments:
For example, max <> (7, 42) // enforces that only the instantiation of the function template is taken into account in the reload parsing, rather than the normal function with the same name.
Generally, do not make too many changes to the signature when you reload the function template: only specify the number of parameters or explicitly specify the template parameter.
Chapter 3. Class templates
3.1.1 declaration of class templates
When you need to use the name of the template class within the class template, you do not need to add the template parameter to limit it (for example, the copy constructor of the vector and the value assignment operator ); when you need to use the template class type, you need to add templater parameter to limit (for example, vector construction and destructor)
3.2 use of class template Stack
For Class templates, only the actually called member functions are instantiated.
3.3 specializations of class templates
If the class template is made special, all member functions must be made special.
3.4 partial Specialization
The class template can be partial (partial sepcializtion), but the function template does not allow
3.5 default template arguments
The class template can have the default template arguments.
Chapter 4. Non-type template parameters
For Class templates and function templates, template parameter is not necessarily of the type
4.1 Non-type class template parameters
Each class template instance has its own independent type. For non-type class template parameters, you can also specify the default value.
4.2 non-type function template parameters
4.3 restrictions for nontype template parameters
Values that can be used for nontype template parameters include constant integer values or pointers to external linkage objects.
Floating point values and classes cannot be used as nontype template parameters
String literals is an object with Internal Linkage (that is, the same string literal
Independent in their respective modules)
Chapter 5. Tricky Basics
5.1 keyword typename
When you want to use a qualified dependent name, you must first add typename to inform the compiler that the latter name is of a type.
The. template construct
When the '.' operator is used for an object dependent on Template parameter, 'template' must be added after'
5.2 using this->
If a class is derived from a template class, it is best to explicitly add 'this-> 'before the function name when calling the base class function in its member functions'
5.3 member templates
Class Members can also be templates, which are valid for both internal classes and member functions.
The template assignment operator in the class does not replace the default assignment operator. When values are assigned between the same types, the default assignment operator is still called.
5.4 template parameters
Note that the real parameter type of template tempalte parameter must be exactly the same as that of the form parameter.
Template template parameters is the new content in the C ++ annotation.
5.5 zero Initialization
T x = T ();
5.6 using string literals as arguments for function templates
The string literal type is related to its length. For example, the "apple" type is const char [6], and the "banana" type is const char [7].
When string literal is passed to the function template by reference, the type of the function template is const char [X]. Only when string literal is passed to the function template by value, the decay of array-to-pointer will occur. The type of the function template is const char *
Chapter 6. Using templates in practice
6.1 The risk sion Model
(Common) Code Organization idioms:
1. Complete information of the class and other types is saved in the header file.
2. for global variables and non-inline functions, the header file contains only its declaration and the definition is put into another. cpp file (this file also contains the previous header file)
If the second method is used for the template, there is usually no problem during compilation, but the corresponding definition of the function (Instance) cannot be found through the connection.
To be correct, a template is instantiated. the compiler must understand both the template definition and the type parameters used for instantiation. In the preceding organization method, the two information is separated, the compiler cannot obtain both.
Cost caused by the regression sion model:
1. The template user not only includes the template, but also includes the header files on which the template depends. This will greatly expand the size of the source code and prolong the Compilation Time.
2. For non-inline function template, it may cause the compiler to generate the same function template instance in two different tu instances. In this way, an error may be reported during connection.
6.2 explicit instantiation
Explicit instantiation Directive
6.2.2 combining the entire sion model and explicit instantiation
Provides two sets of pullover files (one set for implementation and one set for Declaration), which are used for regression sion model and explicit instantiation respectively.
6.3 The Separation Model
The common misunderstanding is that the export mechanism helps to hide the implementation code of the template, just like the header file and binary target file provided by common functions.
No, the purpose of export is not
6.4 templates and inline
Do not put the function template implementation in the header file. The default function template is internal linkage.
6.5 precompiled headers
Some C ++ Standards Committee Members think that precompiled headers such as STD. HPP are very valuable. We recommend that you add them to the labels to become standard header files.
6.6 debugging templates
Chapter 7. Basic Template terminology
7.1 "class template" or "template class "?
Use "class template", avoid "template class"
Use "fucntion template", avoid "template function"
Member function Template
7.2 instantiation and specialization
Instantiation: the process of producing Specialization
Sepcialization: The resuling entity of Instantiation Process
However, instantiation is not the only way to produce specialization.
Primary Template
7.4 The One-definition rule (ODR)
1. Non-inline functions and member functions, as well as global variables and static class members should be defined only in one place throughout the program;
2. The class (and struct and union) and inline functions are defined at most once in each tu, and all definitions should be consistent.
7.5 template arguments versus template parameters
The combination of Template Name, followed by the arguments in angle brackets, is called a template-ID
Arguments-> actual parameters
Parameter-> formal parameters
A basic rule: the template argument must be a number or value that can be determined during the compilation period.