non-type template parameters
1. Template parameters are classified into type parameters and non-type parameters.
2. The type parameter is: appearing in the template parameter list, followed by the parameter type name such as class or typename. Type parameters can also be given default values
. 3. Non-type formal parameters use a constant as a parameter of a class (function) template, and the parameter can be used as a constant in the class (function) template.
4 Non-type template parameters are generally constants and integers, such as int and char types, while floating-point numbers , class objects , and strings are not allowed as non-type template parameters, and an error will be reported if they are written.
5. Non-type template parameters must be able to confirm the result at compile time
There is also an Array in the library, what is the difference between it and the array in c language?
There is no difference at the bottom, but arrays are spot-checked for out-of-bounds checks, while Array is strict for out-of-bounds checks.
template specialization
function template
Here, if the Less function is used, then there is a problem with the second comparison, which is based on the address of p1 and the address of p2.
The first way: function overloading
As shown in the figure, function B constitutes overloading of function A
The second way: function specialization
The specialization steps of a function template:
- There must be a basic function template first
- The keyword template is followed by a pair of empty angle brackets <>
- The function name is followed by a pair of angle brackets, which specify the type to be specialized
- Function parameter table: It must be exactly the same as the basic parameter type of the template function, if it is different, the compiler will report a strange error.
Function D is a specialization of function C (specialization of function)
class template specialization
full specialization, partial specialization
Full specialization is to determinize all the parameters in the template parameter list.
Partial specialization: Any specialization that further conditionally restricts the design of template parameters. semi-specialization
There are two types of partial specialization:
1. Partial specialization: Specialize part of the parameters in the template parameter class table.
2. Further restrictions on parameters: Partial specialization does not just refer to specialization of some parameters, but a specialized version designed for further conditional restrictions on template parameters.
The compiler chooses a more suitable class based on the parameters
Specialization does not just refer to specialization of some parameters, but a specialized version designed for further conditional restrictions on template parameters. **
The compiler chooses a more suitable class based on the parameters
Advantages and disadvantages of templates
[Advantages]
1. The template reuses code, saves resources, and enables faster iterative development, resulting in the creation of the C++ Standard Template Library (STL). 2. Enhances the
flexibility of the code.
[Disadvantages]
1. Templates can cause code expansion problems , it will also lead to longer compilation time
2. When a template compilation error occurs, the error message is very messy and it is not easy to locate the error