尽管类的构造函数参数看起来不一样,但错误“调用‘ ’的构造函数不明确”?

Lio*_*ing 5 c++ compiler-errors class

出现一条错误消息Call to constructor of 'Binary' is ambiguous,该错误消息仅LLVM在 macOS 上使用编译器时出现,但在 Windows 上则不会出现。
此外,该类的构造函数参数看起来也不一样。

class Binary {
public:
    Binary() = default;
    Binary(uintmax_t containerSize);
    Binary(unsigned char binary);
    Binary(std::initializer_list<unsigned char> binaryList);
    // .....
};

// When using
// fileSize is `std::streamoff` data type
Binary fileContent((unsigned long long)fileSize)  // << This line is causing the problem.
Run Code Online (Sandbox Code Playgroud)

我的班级出了什么问题?

cig*_*ien 5

uintmax_t是机器上最大宽度无符号整数类型的 typedef。编译代码时,如果该类型不完全 unsigned long long是,则此调用:

Binary fileContent((unsigned long long)fileSize); 
Run Code Online (Sandbox Code Playgroud)

是不明确的,因为参数需要经过一次转换才能匹配这些构造函数之一:

Binary(uintmax_t containerSize); // conversion from unsigned long long to uintmax_t needed
Binary(unsigned char binary);    // conversion from unsigned long long to unsigned char needed
Run Code Online (Sandbox Code Playgroud)

并且编译器无法在它们之间进行选择,并且会出现错误。

如果恰好uintmax_t是,则第一个构造函数是完全匹配的,并且被选择,并且程序可以编译。据推测,这就是您所看到的 macOS 和 Windows 编译器版本之间的差异。 unsigned long long