How overload resolution decides the best between type and substituted type?

66 Views Asked by At

I have example that works, but I believe it should not

template <typename T>
void foo(int n) {
    std::cout << "foo(int)" << std::endl;
}

template <typename T>
void foo(T t) {
    std::cout << "foo(T)" << std::endl;
}

int main() {
  foo<double>(42.0); // 1
  foo<int>(42);      // 2
}

Live example on Godbolt

For (1) everything is ok, exact template match beats standard conversion double -> int

But for (2) resulting instantiation looks equivalent for both overloads, and I expected error in this case

Still both clang 11 and gcc 10.2 agree that best match for (2) is foo(int), so looks like I don't understand something important

0

There are 0 best solutions below