https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118428

            Bug ID: 118428
           Summary: Inherited virtual base class with a private destructor
                    is not rejected during instantiation of the
                    corresponding derived class
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wangbopku15 at gmail dot com
  Target Milestone: ---

Consider the following code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

struct A{
    virtual ~A();
};

struct B : private virtual A {};
struct C : private B{};

void foo(){
    auto c=C{};
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Clang rejects it with the following diagnostic: 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<source>:6:8: error: inherited virtual base class 'A' has private destructor
    6 | struct C : private B{};
      |        ^
<source>:9:12: note: in implicit default constructor for 'C' first required
here
    9 |     auto c=C{};
      |            ^
<source>:5:12: note: declared private here
    5 | struct B : private virtual A {};
      |            ^~~~~~~~~~~~~~~~~
<source>:6:8: error: inherited virtual base class 'A' has private destructor
    6 | struct C : private B{};
      |        ^
<source>:9:12: note: in implicit destructor for 'C' first required here
    9 |     auto c=C{};
      |            ^
<source>:5:12: note: declared private here
    5 | struct B : private virtual A {};
      |            ^~~~~~~~~~~~~~~~~
2 errors generated.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MSVC with:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<source>(6): warning C4594: class 'C' can never be instantiated - indirect
virtual base class 'A' is inaccessible
<source>(5): note: 'A' is a private base class of 'B'
<source>(6): warning C4624: 'C': destructor was implicitly defined as deleted
<source>(6): error C2282: 'C::~C' cannot override 'B::~B'
<source>(5): note: 'B::~B' is not deleted

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It seems that the instantiation of class 'C' should not be allowed as the
destructor of 'A' is private within 'B'.

But GCC accepts this:

https://godbolt.org/z/qdsz1oxq1

Reply via email to