https://bugs.llvm.org/show_bug.cgi?id=42211

            Bug ID: 42211
           Summary: using declaration on method with multiple inheritance
                    disables virtual call to overridden method
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected], [email protected],
                    [email protected]

Following test case produces different result for clang and other compilers
that I tested. I believe it's bug in clang.

// start
#include <iostream>

struct A {
    virtual void foo() {
        std::cout << "A\n";
    }
};

struct B : virtual A {};

struct C : virtual A {
    void foo() override {
        std::cout << "C\n";
    }   
};

struct D : B, C {
    using A::foo;
};

int main() {
    D d;
    d.foo();

    A &a = d;
    a.foo();

    return 0;
}
// end

On clang (4.0 - 8.0 and trunk on godbolt) output is:
> A
> C

gcc (versions 4.8.1 - 9.1), icc (versions 16, 17, 19), Visual Studio 2017
15.4.0 Preview 1.0, Visual Studio 2013 12.0.31101.00 Update 4, clang (versions
3.4.1 - 3.9.1)
all give following output, which is what I expect:
> C
> C

It seems that only for first derived class (B in this example) foo is called
using virtual call. Base method is used only if called through D object. When
called through reference to A, method from C is used.
If I derive from more classes than B and C, it will behave the same for them as
for C - method from A will be called.

If 'using A::foo' is removed, both calls are made to C::foo.

Fix for this is very important for me because it's hard to identify where such
construction is used in real code and I have to and want to use gcc and clang
for project I'm working on. Only way that I can think of, to find if similar
construction is in more places in codebase is writing AST matcher and writing
some workaround for found cases.


Discussion with bigger example on stackoverflow:
https://stackoverflow.com/questions/56452518/virtual-function-overloading-in-diamond-hierarchy-produces-different-results-in

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to