Question

Overload resolution between ordinary and explicit object member functions

In the following test program, struct B has two member functions f, which can be called using B{}.f(): one ordinary f() and another with explicit object f(this A).

struct A {
    int f() { return 1; }
};

struct B : A {
    using A::f;
    int f(this A) { return 2; }
};

int main() {
    return B{}.f();
}

Which function must be selected by overload resolution?

GCC and MSVC prefer ordinary member function f() and the program returns 1.

But Clang makes the opposite choice, selecting explicit object member function f(this A) and the program returns 2.

Online demo: https://gcc.godbolt.org/z/1bo69Ta8q

Which compiler is correct here if any (why not ambiguity of overload resolution)?

 2  95  2
1 Jan 1970

Solution

 2

tldr; Clang is wrong in choosing the explicit object member function for the reason(s) explained below. Here is the confirmed clang bug.

Which compiler is correct here if any (why not ambiguity of overload resolution)?

First note that as per over.match.func.general, the function nominated by using A::f is considered to be a member of the derived class for the purposes of defining the type of the implicit object parameter.

This means that for the call B{}.f(), the implicit object member function is a better match because it doesn't require conversion for the argument B{}.

For reference here is over.match.func.general that states:

For non-conversion functions introduced by a using-declaration into a derived class, the function is considered to be a member of the derived class for the purpose of defining the type of the implicit object parameter.


We can also see this by adding a copy ctor for class A and noticing that clang incorrectly uses the copy ctor for the conversion but gcc and msvc do not. Demo

Thus, gcc and msvc are right in choosing the implicit version that was made visible by using A::f;.


Here is the confirmed clang bug:

Clang chooses the incorrect overload when explicit and implicit member functions are involved

2024-07-22
user12002570