C++ Logo

std-discussion

Advanced search

Re: Legal? Incomplete type "gains" base class on completion.

From: Fabio Fracassi <f.fracassi_at_[hidden]>
Date: Mon, 13 May 2019 00:27:35 +0200
On 12.05.19 21:29, Nicolas Lesser wrote:
> Fabio Fracassi via Std-Discussion wrote:
>> consider the following (also on https://gcc.godbolt.org/z/6ikNyq):
>>
>> structS; // (1)
>>
>> structU {
>> S* sp_;
>> U(S* sp) : sp_{sp} {}
>> autop() -> int;
>> };
>>
>> structB {
>> inti_ = 42;
>> autop(inta) -> int{ returna + i_; }
>> };
>>
>> structS : B { // (2) Type S is completed here ... legal to introduce B
>> as base?
>> autop() -> int{ returnB::p(3); }
>> };
>>
>> autoU::p() -> int{ returnsp_->p(); }
>>
>> intmain() {
>> S s{};
>> U u{&s};
>> returnu.p();
>> }
>>
>> Is this legal? Reading the standard I do not find anything that either
>> forbids it, but neither do I find anything that allows it.
>> All compilers that I tried are fine with it, but does anyone know
>> whether this is intentional?
>>
>>
> Why would it be ill-formed? Also which part exactly are you having a problem with? Is it struct S : B?
>
> If so: There is nothing special going on here that would need special rules in the standard. Just the normal rules for base classes and inheritance make it legal to make S derive from B. It doesn't matter that you are using S before as a pointer.
>
No problem. I guess I confused myself over how incomplete classes work
(I wondered if the forward decl needed to know the base class). I just
rechecked and found that the forward declaration is indeed just the name
(the base class cannot be part of the forward decl see [baseic.def]), so
as you say: it just falls out of the normal rules.

Received on 2019-05-12 17:29:43