Call rules and general function of c ++ template functions

1. If the template function and normal function can be achieved, the priority call ordinary function.

2. The call can be forced by an empty template function template parameter list;

3. function templates can be overloaded;

4. If the function templates can produce a better match, priority call function template;

#include<iostream>
using namespace std;

int myAdd(int a, int b) {
    cout << "调用普通函数" << endl;
    return a + b;
}
template<class T>
T myAdd(T a, T b) {
    cout << "调用函数模板" << endl;
    return a + b;
}
template<class T>
T myAdd(T a, T b,T c) {
    cout << "Call overloaded function template " << endl;
     return A + B; 
} 
void Test () {
     int A = 10 ;
     int B = 20 is ;
     // . 1 where an ordinary function call 
    COUT << myAdd (A, B) << endl ;
     // 2 where the function call template 
    COUT << myAdd <> (A, B) << endl;
     // . 3 overloaded function call template here 
    COUT << myAdd (A, B, 100 ) << endl;
     // . 4 here, the calling function template 
    COUT << myAdd ( ' A ' , ' B ') << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/xiximayou/p/12106230.html