Common overload functions can be deduced through function parameters, and the compiler selects the most suitable overload function as a candidate function. Similarly, a template function can present the actual type of the model parameter through the type of the function parameter. The C ++ compiler has the following skills and precautions in completing type deduction. Here we will try our best to list the most common rules and give corresponding examples for your understanding.
1. deduct the most basic template function type. See the following code examples, key comments, and output results.
1 # include <stdio. h> 2 # include <typeinfo. h> 3 4 template <typename T> 5 void func1 (T * t) {6 printf ("The type is % s \ n", typeid (t ). name (); 7} 8 9 template <typename T, int N> 10 void func2 (T (&) [N]) {11 printf ("The type of T is % s, N equals to % d. \ n ", typeid (T ). name (), N); 12} 13 14 template <typename T1, typename T2, typename T3> 15 void func3 (T1 (T2: *) (T3 *)) {16 printf ("The type of T1 is % s \ n", typeid (T1 ). n Ame (); 17 printf ("The type of T2 is % s \ n", typeid (T2 ). name (); 18 printf ("The type of T3 is % s \ n", typeid (T3 ). name (); 19} 20 21 template <int N> 22 class TemplateTest {23 public: 24 typedef int INT; 25 void f (int) {}26 }; 27 28 template <int N> 29 void func4 (void (TemplateTest <N >:: * p) (typename TemplateTest <N >:: INT )) {30 printf ("N equals to % d \ n", N); 31} 32 33 class Test {34 public: 35 void f (double *) {} 36 }; 37 38 int main () {39 int ** pp = NULL; 40 func1 (pp); // T type: int * 41 42 bool B [100]; 43 func2 (B); // The type of T is bool. The value of N is 10044 45 func3 (& Test: f); // T1: void, T2: class Test, t3: double. 46 47 func4 (& TemplateTest <200 >:: f); 48 return 0; 49} 50 // The type is int ** 51 // The type of T is bool, N equals to 100.52 // The type of T1 is void53 // The type of T2 is class Test54 // The type of T3 is double55 // N equals to 200
2. If the function parameter is T & (reference type), its modulo parameter type is still T.
1 #include <stdio.h> 2 #include <typeinfo.h> 3 4 template<typename T> 5 void func(T& s) { 6 printf("The type is %s\n",typeid(s).name()); 7 } 8 9 int main() {10 int k = 0;11 func(k);12 return 0;13 }14 //The type is int
From the output, we can see that the actual type of T is still int, not int &.
3. If the function parameter is of the reference type, and the function parameter is of the array or function type, no implicit conversion will be performed. If the parameter is not of the reference type, an implicit type conversion from the array to the pointer and function to the function pointer is generated.
1 #include <stdio.h> 2 #include <typeinfo.h> 3 4 template<typename T> 5 void func(T const& s) { 6 printf("The type is %s\n",typeid(s).name()); 7 } 8 9 template<typename T>10 void func2(T s) {11 printf("The type is %s\n",typeid(s).name());12 }13 14 int main() {15 int k[5];16 func(k);17 func2(k);18 return 0;19 }20 //The type is int const [5]21 //The type is int *
From the output, we can see that the function parameters of the reference type still maintain the array type, while functions of the non-reference type convert the type of the real parameter to the pointer type.
4. Several special examples of template deduction.
1 # include <stdio. h> 2 # include <typeinfo. h> 3 4 template <typename T> 5 void func (T) {6 printf ("The type is % s \ n", typeid (T ). name (); 7} 8 9 void (* pf) (char) = & func; // type deduction is completed in this assignment. 10 11 int main () {12 pf ('A'); 13 return 0; 14} 15 // The type is char
5. if exact match is not found during deduction, but the function parameter is of the base class type (or pointer), the deduction still succeeds when the real parameter is of the derived class type (or pointer.
1 #include <stdio.h> 2 #include <typeinfo.h> 3 4 template<typename T> 5 class Base { 6 }; 7 8 template<typename T> 9 class Derive : public Base<T> {10 };11 12 template<typename T>13 void func(Base<T>*) {14 printf("The type of T is %s.\n",typeid(T).name());15 printf("The type of Base<T> is %s.\n",typeid(Base<T>).name());16 }17 18 int main() {19 Derive<int> d;20 func(&d);21 return 0;22 }23 //The type of T is int.24 //The type of Base<T> is class Base<int>.
From the above output, we can see that the T type is successfully deduced, even if the real parameter of the func function is Derive <int> (Base <int> derived class ).
6. The default parameters of the function cannot be involved in type deduction.
1 # include <stdio. h> 2 3 template <typename T> 4 void func (T x = 100) {5 6} 7 8 int main () {9 func <int> (); // The parameter type of the specified function is displayed here, so you can compile it. 10 func (100); // The type of the model parameter of the function is pushed by the type of the real parameter, or compiled. 11 func (); // you can use this rule to deduct the model parameter type because the default value of the function parameter is 100. 12 return 0; 13}