博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++重载的时候会优先匹配非模块函数
阅读量:6373 次
发布时间:2019-06-23

本文共 4036 字,大约阅读时间需要 13 分钟。

发现当重载函数足够多的时候,

出现可以符合非模块函数,及模块函数都可匹配的时候,

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", "asfdksdljjjjjjjjjjjjjjjjjjjjjjjjj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jjjjjjjjjjjjjjjjjjjjjjjjjjjj"}; 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.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证明调用的是非模块函数

转载于:https://www.cnblogs.com/vimmer/archive/2012/12/15/2819612.html

你可能感兴趣的文章
海哥:再谈粉丝经济,你所知道的99%都是错误的。
查看>>
内涵图让你读懂社会
查看>>
awk学习笔记
查看>>
Spring 学习之bean的理解
查看>>
【不定期更新】游戏开发中的一些良好习惯与技术技巧
查看>>
DNS的初步了解
查看>>
多线程核对MD5码脚本
查看>>
LINUX 命令ifconfig 无效
查看>>
MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
查看>>
Oracle 11g安装过程中错误解决
查看>>
JavaScript强化教程——jQuery AJAX 实例
查看>>
Java中HashMap,LinkedHashMap,TreeMap的区别
查看>>
iPhone消息推送机制实现与探讨(转)
查看>>
iphone 线程 NSCondition NSThread
查看>>
Debian8添加kali源并安装metasploit
查看>>
Linux redhat 5.7 安装 Teamviewer7
查看>>
android EditText inputType说明
查看>>
交叉熵代价函数(作用及公式推导)
查看>>
这个用markdown编写
查看>>
display、 float 、position
查看>>