Why does MSVC 16.7 treat function overload involving conversion from long to int ambiguous?

Viewed 85

I have updated my MSVC to 16.7 and now he complains about the following code:

error C2668: ... ambiguous call to overloaded function

void f(int){
}

void f(double){
}

int main()
{   
    long l = 5;
    static_assert(sizeof(long)==4,"");
    f(l);
}

It used to compile in older compilers https://godbolt.org/z/PG7GTY

I understand why GCC and Clang complain, since their long is 8 bytes, but why would MSVC reject this code when long on MSVC is 32bit, aka same as int.

Does C++ standard requires this to be ambiguous, or is it fine for implementation to consider long as int for purposes of overload resolution if the are "same underlying" type.

If it matters I am compiling with C++20 flag on MSVC.

1 Answers

First the compiler looks for a function that can be called without implicit conversion. If it finds exactly one it calls it.

If it finds none it looks for a function that can be called with one implicit conversion. In your example both functions need exactly one implicit conversion. That makes the code ambiguous.

The size of the types is absolutely irrelevant.

https://en.cppreference.com/w/cpp/language/overload_resolution#Viable_functions

Related