发现当重载函数足够多的时候,
出现可以符合非模块函数,及模块函数都可匹配的时候,
GCC好像会优先匹配非模块函数.
见下面代码
#include#include #include #include using namespace std;int max(const int n1, const int n2){ return n1 > n2 ? n1 : n2;}float max( const float f1, const float f2){ return f1 > f2 ? f1 : f2;}string max(const string str1, const string str2){ return str1 > str2 ? str1 : str2;}#if 0int max(const vector & vec){ return *max_element(vec.begin(), vec.end());}float max(const vector & vec){ return *max_element(vec.begin(), vec.end());}string max(const vector & vec){ return *max_element(vec.begin(), vec.end());}int max(const int* array, int size){ int int_max = numeric_limits ::min(); for(int i = 0; i < size; ++i) { int_max = max(int_max, array[i]); } return int_max;}float max(const float* array, int size){ float float_max = numeric_limits ::min(); for(int i = 0; i < size; ++i) { float_max = max(float_max, array[i]); } return float_max;}string max(const string* array, int size){ return *max_element(array, array + size);}#endiftemplate T max(const vector & vec) { cout << "invoke" < T max(const T* array, int size){ return *max_element(array, array + size);}int main(int argc, const char *argv[]){ cout << "test overload max" << endl; int n1 = 3; int n2 = 9; float f1 = 3.14; float f2 = 98.44; string str1 = "s1"; string str2 = "abc"; cout << "max int (" << n1 << " " << n2 << " )" << max(n1, n2) << endl << " max float (" << f1 << " " << f2 << " ) " << max(f1, f2) << endl << "max string ( " << str1 << " " << str2 << " ) " << max(str1, str2) << endl; int int_array[] = { 33,444,5,322,124,55,3436}; vector int_vec (int_array, int_array + sizeof(int_array)/ sizeof(int)); cout << " the max number of the int vector is : " << max(int_vec) << endl; float f_array[] = { 34.24, 4324.43, 9999.43, 556.34}; vector f_vec (f_array, f_array + sizeof(f_array) / sizeof(float)); cout << " the max number of the float vector is : " << max(f_vec) << endl; string str_array[] = { "boog", "gogod", "zero", "loe", "abcdasdfas", "asfdksdljjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"}; vector str_vec (str_array, str_array + sizeof(str_array) / sizeof(string)); cout << " the max number of the string vector is : " << max(str_vec) << endl;#if 1 cout << "the max number of the int array is :" << max( int_array, sizeof(int_array)/ sizeof(int)) <
output
test overload max
max int (3 9 )9 max float (3.14 98.44 ) 98.44max string ( s1 abc ) s1invoke the max number of the int vector is : 3436invoke the max number of the float vector is : 9999.43invoke the max number of the string vector is : zerothe max number of the int array is :3436the max number of the flaot array is :9999.43the max number of the string array is :zero sizes = 6 sizes of str_array 24 sizes of str_array[0] 4
如果把#if 0 打开,,那么就不会出现invoke证明调用的是非模块函数