Re-learning C ++ (12) templates' special and biased features
1. template Definition 1. class template
Template
Class compare {public: bool IsEqual (T t1, T t2) {return t1 = t2 ;}}; int main () {char str1 [] = "Hello "; char str2 [] = "Hello"; compare
C1; compare
C2; cout <c1.IsEqual (1, 1) <endl; // compare two int-type parameters: cout <c2.IsEqual (str1, str2) <endl; // compare two char * type parameters return 0 ;}
2. Function Template
bool IsEqual(T t1, T t2){ return t1 == t2;}int main(){ char str1[] = "Hello"; char str2[] = "Hello"; cout << IsEqual(1, 1) << endl; cout << IsEqual(str1, str2) << endl; return 0;}
Ii. Special Template
In the above Code, compare whether the strings are equal. Because the input parameters are of the char * type, the IsEqual function template simply compares the values of the input parameters, that is, whether the two pointers are equal, therefore, the result is false. Obviously, this is inconsistent with our original intention. Therefore, the preceding template requires special processing of the char * type, that is, special.
1. Special Class Template
Template <> // tell the compiler that this is a special template class compare
// Special (char *) {public: bool IsEqual (char * t1, char * t2) {return strcmp (t1, t2) = 0; // use strcmp to compare strings }};
Note: If you are a class-specific external definition member, you cannot add a template <> tag before the member.
In addition, we can only specialize in members rather than special classes. For example, in vector, we can only specialize in push_back operations:
template<>void vector
::push_back(const char *const &val){ //...}
Class Type vector
2. Special Function Template
Template <> bool IsEqual (char * t1, char * t2) // function template specialization {return strcmp (t1, t2) = 0 ;}
Iii. Normalization of templates 1. Normalization of class templates
When there are multiple template parameters, we can only specify a part of them instead of all.
The definition of vector class in the c ++ standard library is an example:
template
class vector { // … // };template
class vector
{ //…//};
In the preceding example, a parameter is bound to the bool type, and the other parameter is not bound yet, which must be specified by the user.
2. Function template "special"
Strictly speaking, function templates do not support partial features. However, functions can be overloaded to achieve partial features similar to class templates.
template
void f(T); (a)
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 (.
Iv. template specialization Matching 1. class template matching
Optimized is superior to secondary-specific, that is, the most precise matching of template parameters has the highest priority.
Example:
Template
Class vector {//... //}; // (A) Common template
Class vector
{//... ///}; // (B) Special template for pointer type <> class vector
{//... //}; // (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. Function template matching
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
Void f (T); // (d) template
Void f (int, T, double); // (e) template
Void f (T *); // (f) template <> void f
(Int); // (g) void f (double); // (h) bool B; int I; double d; f (B ); // call (d) f (I, 42, d) with T = bool // call (e) f (& I) with T = int ); // call (f) f (d) with T = int *; // call (g)