An error "Call to constructor of ' ' is ambiguous", although the class's constructor parameters do not look the same?

Viewed 141

There is an error message that appears Call to constructor of 'Binary' is ambiguous, that error message just appears when using LLVM compiler on macOS but on windows, it doesn't appear.
Also, the class's constructor parameters do not look the same.

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.

What's wrong with my class?

1 Answers

uintmax_t is a typedef for the maximum-width unsigned integer type on your machine. When compiling your code, if that type is not exactly unsigned long long, then this call:

Binary fileContent((unsigned long long)fileSize); 

is ambiguous, since the argument will need to undergo exactly one conversion to match either one of these constructors:

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

and the compiler can't choose between them, and there's an error.

If uintmax_t happens to be exactly unsigned long long, then the 1st constructor is an exact match, and is chosen, and the program compiles. Presumably, this is the difference between the macOS, and Windows compiler version that you are seeing.

Related