国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

CppQuiz 系列3

2019-11-08 02:14:00
字體:
供稿:網(wǎng)友

3. 參數(shù)匹配

#include <iostream>void f(int) { std::cout << 1; }void f(unsigned) { std::cout << 2; }int main() { f(-2.5);}

答案是:CE

在選擇重載函數(shù)時(shí),如果沒有完全匹配的,就會(huì)通過轉(zhuǎn)換選擇一個(gè)次匹配的,轉(zhuǎn)換的種類如表中所示,題中所給的轉(zhuǎn)換是屬于浮點(diǎn)到整形的轉(zhuǎn)換。相比之下沒有更好的,所以編譯器不知道選擇哪個(gè)重載函數(shù),所以是ill-formed。

這里寫圖片描述

This overload is ambiguous. Why?

There are two viable functions for the call f(-2.5). For the compiler to select one, one of them needs to be better than the other, or the PRogram is ill-formed. In our case, they are equally good, making the program ill-formed.

According to §13.3.3 in the standard, a viable one-argument function is better than another if the conversion sequence for the argument is better. So why isn’t the int conversion sequence better than the unsigned conversion sequence, given that the double is signed?

All conversions are given a rank, and both “double => int” and “double => unsigned int” are of type “floating-integral conversion”, which has rank “conversion”. See Table 12 in the standard and §4.9. Since they have the same rank, no conversion is better than the other, and the program is ill-formed.


接上期,題中一共列出了4種函數(shù)聲明和兩種參數(shù)形式,列表如下:

函數(shù)聲明&參數(shù)類型 const char [] const char *
const std::string & Conversion Conversion
const void * Array to pointer Pointer Conversion
template T &s T=const char &[] T=const char * &
templateconst char (&)[N] Identity 不匹配

可見,對(duì)于T&來說,對(duì)兩種參數(shù)都是屬于Exact Match,而偏特化版本對(duì)const char[] 是Identity,而對(duì)const char * 不匹配,結(jié)果一目了然,而const void * 和 const std::string & 都需要進(jìn)行轉(zhuǎn)換,所以,const char [] 匹配 const char (&)[N],const char * 匹配T&.


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 馆陶县| 甘谷县| 太白县| 肇源县| 淮安市| 苏尼特左旗| 黄平县| 常州市| 洛阳市| 林周县| 奈曼旗| 棋牌| 黄冈市| 临海市| 庐江县| 布尔津县| 疏勒县| 将乐县| 永川市| 汾阳市| 康乐县| 收藏| 元氏县| 鹰潭市| 搜索| 浦城县| 无锡市| 原平市| 高密市| 文山县| 双桥区| 长岛县| 石家庄市| 运城市| 肇庆市| 竹北市| 多伦县| 科技| 伊宁市| 大新县| 新乡市|