国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 湟源县| 崇文区| 清原| 渭源县| 商南县| 札达县| 邢台县| 敖汉旗| 通河县| 东至县| 防城港市| 瑞丽市| 平利县| 翁牛特旗| 滨州市| 镇赉县| 芒康县| 安丘市| 益阳市| 特克斯县| 荥经县| 秀山| 河东区| 上杭县| 汝南县| 万山特区| 尤溪县| 富源县| 马尔康县| 洞口县| 广汉市| 资阳市| 瓦房店市| 石城县| 永年县| 灵山县| 那坡县| 双柏县| 富源县| 宝兴县| 汉寿县|