C++重载的时候会优先匹配非模块函数
发现当重载函数足够多的时候,
出现可以符合非模块函数,及模块函数都可匹配的时候,
GCC好像会优先匹配非模块函数.
见下面代码
#include <iostream> #include <vector> #include <string> #include <limits> 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 0 int max(const vector<int>& vec) { return *max_element(vec.begin(), vec.end()); } float max(const vector<float>& vec) { return *max_element(vec.begin(), vec.end()); } string max(const vector<string>& vec) { return *max_element(vec.begin(), vec.end()); } int max(const int* array, int size) { int int_max = numeric_limits<int>::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<float>::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); } #endif template <typename T> T max(const vector<T>& vec) { cout << "invoke" <<endl; return *max_element(vec.begin(), vec.end()); } template <typename T> 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> 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<float> 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", "asfdksdljjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj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jjjjjjjj"}; vector<string> 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)) <<endl; cout << "the max number of the flaot array is :" << max( f_array, sizeof(f_array)/ sizeof(float)) <<endl; cout << "the max number of the string array is :" << max( str_array, sizeof(str_array)/ sizeof(str_array[0])) << " sizes = " << sizeof(str_array)/ sizeof(str_array[0]) << " sizes of str_array " << sizeof(str_array ) << " sizes of str_array[0] " << sizeof(str_array[0] ) << endl; #endif return 0; }
output
test overload max
max int (3 9 )9
max float (3.14 98.44 ) 98.44
max string ( s1 abc ) s1
invoke
the max number of the int vector is : 3436
invoke
the max number of the float vector is : 9999.43
invoke
the max number of the string vector is : zero
the max number of the int array is :3436
the max number of the flaot array is :9999.43
the max number of the string array is :zero sizes = 6 sizes of str_array 24 sizes of str_array[0] 4
如果把#if 0 打开,,那么就不会出现invoke证明调用的是非模块函数