答案是: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&.
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注