I. Defining a template
1.1 Function templates
1. Where applicable: If two functions are almost identical, the only difference is the type of the parameter, and the function body is exactly the same.
2. Definition
Templates < template parameter list (comma delimited) >
1 template <typename t>2int Compare (const T&V1,const t& v2) 3 {4 if return -1; 5 return1; 6 }
3. Instantiating a function template
When a function template is called, the compiler infers the argument with a function argument.
4. Template parameter types
Purpose of type parameter T: Specify return type, specify function parameter type, use in function body for variable declaration, variable type conversion
5. Non-type template parameters
A non-type parameter represents a value rather than a type. When a template is instantiated, the non-type parameter is replaced by a user-supplied or compiler-inferred value that must be a constant expression.
Purpose: Within the template definition, the template non-type parameter is a constant value. In places where constant expressions are required, you can use non-type parameters, such as specifying the array size.
//Write a conpare version processing string literal constant. //since an array cannot be copied, we define the parameter as a reference to the arraysTemplate <unsigned n,unsigned m>intCompareConst Char(&P1) [N],Const Char(&p2) [M]) { returnstrcmp (P1,P2);}//Call CompareCompare"Hi","mom");//The compiler inserts a null character as Terminator at the end of the string literal constant, so the compiler instantiates the following versionintCompareConst Char(&P1) [3],Const Char(&P2) [4])
6. function templates for inline and constexpr
Template <typename t> inline T min (const t&,const t&)//Note inline position
7. Coding Principles
The Compare function writes the principle of generic code: the parameter in the template is a const reference; the condition in the function body is judged by using only < comparison operation;
A principle: A template program should minimize the need for an actual parameter type.
8. Template compilation
Attention:
1) The compiler generates code only when we instantiate a specific version of the template.
2) The definition of function templates and class template member functions is usually placed in the header file.
3) Most compilation errors occur during instantiation of the report. It is the responsibility of the caller to ensure that the arguments that are passed to the template support the actions required by the template, and that these operations work correctly.
Second, class template
Unlike function templates, the compiler cannot infer parameter types for class templates. In order to use the class template, additional information must be provided in < > after the template name.
1. Purpose: Unlike classes, templates can be used for more types of elements.
2. Definition
Template <typename t>class blob{public: typedef T VALUE_TYPE; typedef typename Std::vector<T>:: Size_type size_type; Blob (); T& Back (); Toperator[] (Size_type i); Private : std::shared_ptr<std::vector<T>> data; void Check (size_type i,const std::stringconst;
c++--Template and generic programming