For a function template, the compiler uses the real parameters of the called function to determine its function template. The process of determining the real parameters of the template from the real parameters of the function is called the real parameter derivation of the template.
For example:
1 # include <iostream> 2 # include <string> 3 using namespace STD; 4 5 template <class T> 6 int compare (const T & V1, const T & V2) {7 if (V1> V2) {8 cout <"bigger" <Endl; 9 return 0; 10} 11 else {12 cout <"small" <Endl; 13 return 1; 14} 15} 16 17 template <class T> 18 int compare1 (T V1, t V2) {19 if (V1> V2) {20 cout <"compare1: bigger" <Endl; 21 return 0; 22} 23 else {24 cout <"compare1: Small" <Endl; 25 return 1; 26} 27} 28 29 int main () {30 compare (0, 1); // call compare (const Int &, const Int &) 31 compare (3.14, 2.7); // call compare (const float &, const float &) 32 compare (2, 2.7); // error33 34 System ("pause "); 35 return 0; 36}
During the real parameter derivation process of the template, the compiler uses the real parameter type in the function call to find the real parameter of the template. The function version generated by the real parameters of these templates matches the given function call most.
I. template deduction rules
There are also the following rules for the real parameter derivation of the template:
1. if multiple parameters of a function are of the same template type, the inferred parameters must be of the same type.
Like compare (2, 2.7); In the above example, the matching must be completed.
2. If the form parameter of the template function is of a non-reference type, ignore Const. Example:
String S1 ("A"); const string S2 ("AB"); compare1 (S1, S2); // call compare1 (string, string)
3. If the parameter is a const reference or pointer, the compiler automatically converts the parameter to const if it is a non-const reference or pointer. Example:
String S1 ("A"); const string S2 ("AB"); compare (S1, S2); // call compare (const string &, const string &)
4. Non-reference template parameters can convert real parameters of the array or function type into array pointers or function pointers. Example:
Int A [10], B [11]; compare1 (a, B); // call compare1 (int *, int *)
5. The reference type template parameters cannot convert the real parameters of the array or function type into array pointers or function pointers. The parameter derivation will fail. Example:
int a[10], b[11]; compare(a, b); // error
6. The real parameters of the template can be inferred based on the function pointer.
7. The order of real parameter inference in the template starts from the return value of the function and derives one by one from left to right.
Ii. Derivation of templates with different real parameters
In the preceding example, if multiple parameters of a function are of the same template type, the inferred parameters must be exactly the same. Otherwise, an error occurs.
However, we can use multiple types of parameters for derivation. Example:
1 # include <iostream> 2 # include <string> 3 using namespace STD; 4 5 template <typename T, typename B> 6 int compare (const T & V1, const B & V2) {7 if (V1> V2) {8 cout <"bigger" <Endl; 9 return 0; 10} 11 else {12 cout <"small" <Endl; 13 return 1; 14} 15} 16 17 int main () {18 long l = 655555; 19 compare (L, 2); // call (const long &, const Int &) 20 21 system ("pause"); 22 return 0; 23}
[014] template-Derivation of template parameters