C_and_CPP 板


LINE

开发平台(Platform): (Ex: Win10, Linux, ...) Linux 编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出) G++ 问题(Question): C++ function overloading 的一些问题 根据C++ Primer 5e 6.6.1, function matching 共分为5级 1.exact match, include a top-level const is added or discarded e.g. void f(int n) {} int a = 0; f(a); /////////////////////////////// void f(const int n) {} int a = 0; f(a); /////////////////////////////// void f(int n) {} const int a = 0; f(a); 2.match through a const conversion e.g. void f(const int &n) {} int a = 0; f(a); /////////////////////////////// void f(const int *pn) {} int a = 0; int *pa = &a; f(pa); 3.match through a promotion e.g. void f(int n) {} short a = 0; f(a); 4.match through a build-in type or pointer conversion e.g. void f(double dn) {} int a = 0; f(a); /////////////////////////////// void f(void *p) {} int a = 0; int *pa = &a; f(pa); 5.match through a class-type conversion e.g. void f(Base n) {} Derived a; f(a); /////////////////////////////// void f(int n) {} Integer a(0); f(a); // suppose 'Integer' has 'operator int() const;' 各层级例子是我自己举的,可能理解有错误... 回到overload 当多个函数viable且有多个函数同时在最高的层级就会产生ambiguous call 所以以下code让我很疑惑 /////////////////////////////////////// void f(const int &n) {} // f1 void f(int n) {} // f2 int main() { int a = 0; f(a) // error:ambiguous, but f1 is level 2, f2 is level 1. } /////////////////////////////////////// 把f2 加个reference就不会ambiguous /////////////////////////////////////// void f(const int &n) {} // f1 void f(int &n) {} // f2 int main() { int a = 0; f(a) // ok:call f2, f1 is level 2, f2 is level 1. } /////////////////////////////////////// pointer版本的也不会ambiguous /////////////////////////////////////// void f(const int *n) {} // f1 void f(int *n) {} // f2 int main() { int a = 0; int *pa = &a f(pa) // ok call f2, f1 is level 2, f2 is level 1. } /////////////////////////////////////// 所以第一段code为什麽会ambiguous? --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 118.169.35.233
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1547741798.A.0D2.html
1F:推 AstralBrain: 我不知道书里面怎麽写的, 不过c++标准是说只分三级 01/18 03:08
2F:→ AstralBrain: https://i.imgur.com/dbcIwcJ.png 01/18 03:08
3F:→ AstralBrain: Qualification Conv.和Identity都是Exact Match 01/18 03:09
4F:→ AstralBrain: 所以你的第一个code会ambiguous 01/18 03:09
5F:→ AstralBrain: 後面两个不会是因为同Rank还有其他规则细分 01/18 03:09
6F:→ bibo9901: C++跟本没有东西是简单的 ... 01/19 07:21
7F:推 Jockey66666: 有阿 放弃很简单 01/19 09:11







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP