[llvm-bugs] [Bug 42211] New: using declaration on method with multiple inheritance disables virtual call to overridden method

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Jun 9 16:02:25 PDT 2019


            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: unassignedclangbugs at nondot.org
          Reporter: michalbreiter at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

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;

    A &a = d;

    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:

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190609/5136933b/attachment.html>

More information about the llvm-bugs mailing list