運行此代碼時,Clang(3.9.1)和GCC(7,快照)將「1」,「2」打印到控制檯。MSVC是否有權找到此方法調用模糊,而Clang/GCC不?
但是,MSVC無法編譯此代碼:
source_file.cpp(15): error C2668: 'Dictionary::set': ambiguous call to overloaded function
source_file.cpp(9): note: could be 'void Dictionary::set(int64_t)'
source_file.cpp(8): note: or 'void Dictionary::set(const char *)'
source_file.cpp(15): note: while trying to match the argument list '(const unsigned int)'
#include <iostream>
static const unsigned ProtocolMajorVersion = 1;
static const unsigned ProtocolMinorVersion = 0;
class Dictionary {
public:
void set(const char *Str) { std::cout << "1"; }
void set(int64_t val) { std::cout << "2"; }
};
int main() {
Dictionary dict;
dict.set(ProtocolMajorVersion);
dict.set(ProtocolMinorVersion);
}
我認爲MSVC是正確的 - 的ProtocolMajorVersion
值0
,它可以是NULL
或int64_t(0)
。
然而,這似乎是這樣與
dict.set(0);
source_file.cpp:15:10: error: call to member function 'set' is ambiguous dict.set(0);
source_file.cpp:8:10: note: candidate function
void set(const char *Str) { std::cout << "1"; }
source_file.cpp:9:10: note: candidate function
void set(int64_t val) { std::cout << "2"; }
那麼,什麼是怎麼回事更換
時 - 這編譯器是正確的?如果GCC和Clang都接受不正確的代碼,或者MSVC僅僅是越野車,會令我感到驚訝嗎?請參考標準
它是不明確的,因爲類型不匹配,所以它可能是。 'unsigned'的大小不一定是64位,所以它不太可能匹配64_t類型。就我個人而言,我更喜歡MSVC的方式 - 在C/C++程序中注意含糊不清總是件好事。 –