Inheriting constructors from virtual base classes

40 views
Skip to first unread message

Tom Honermann

unread,
Aug 19, 2016, 11:55:15 AM8/19/16
to std-dis...@isocpp.org
While working on some tests recently, I ended up writing the following
test case to exercise inheriting constructors from virtual base
classes. For both D1 and D2, the constructors are inherited from an
immediate base class. I think it makes sense to reject the declaration
of d2 since the D2 constructors will attempt to invoke B's deleted
default constructor. But, there is implementation divergence regarding
the declaration of d1. Gcc bug?

$ cat t.cpp
struct B { B(int); };
struct D1 : virtual B {
using B::B;
};
struct D2 : virtual D1 {
using D1::D1;
};
D1 d1(1); // gcc rejects
D2 d2(2); // clang and gcc reject

Clang trunk (r278952) rejects the declaration of d2:

$ clang -c -std=c++1z t2.cpp
t.cpp:9:4: error: constructor inherited by 'D2' from base class 'B' is
implicitly deleted
D2 d2(2);
^
t.cpp:2:13: note: constructor inherited by 'D2' is implicitly deleted
because base class 'B' has no default constructor
struct D1 : virtual B {
^

Gcc trunk (r238591) rejects the declarations of both d1 and d2:

$ g++ -c -std=c++1z t.cpp
t.cpp:8:8: error: use of deleted function âD1::D1(int)â
D1 d1(1);
^
t.cpp:3:12: note: âD1::D1(int)â is implicitly deleted because the
default definition would be ill-formed:
using B::B;
^
...
x.cpp:9:8: error: use of deleted function âD2::D2(int)â
D2 d2(2);
^
x.cpp:6:13: note: âD2::D2(int)â is implicitly deleted because the
default definition would be ill-formed:
using D1::D1;
^~
...

Tom.

Tom Honermann

unread,
Aug 19, 2016, 11:59:33 AM8/19/16
to std-dis...@isocpp.org
On 8/19/2016 11:54 AM, Tom Honermann wrote:
While working on some tests recently, I ended up writing the following test case to exercise inheriting constructors from virtual base classes.  For both D1 and D2, the constructors are inherited from an immediate base class.  I think it makes sense to reject the declaration of d2 since the D2 constructors will attempt to invoke B's deleted default constructor.  But, there is implementation divergence regarding the declaration of d1.  Gcc bug?
Looks like it is:
- Bug 58751 - [C++11] Inheriting constructors do not work properly with virtual inheritance.
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58751

Tom.

Richard Smith

unread,
Aug 19, 2016, 2:15:47 PM8/19/16
to std-dis...@isocpp.org
Under p0136r1, both initializations should be valid. GCC doesn't implement that yet, and it looks like you've found a bug in Clang's implementation of it. 
Reply all
Reply to author
Forward
0 new messages