C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Partial forward class declarations

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 30 Dec 2024 14:48:23 -0300
On Monday 30 December 2024 11:10:37 Brasilia Standard Time Tiago Freire via
Std-Proposals wrote:
> I guess the answer is Yes and No.
> The question is when you define
> struct D:B
> Is B guaranteed to be aligned at offset 0 within D?
> If the answer is yes, then probably. If the answer is no, then no.
> Tough I assume this would also have an implication in code generation as
> certain methods marked as final can sometimes be optimized by directly
> replacing the address of the final method instead of looking at the vtable.
> And since you can't always see if a method is re-declared final or not it
> will have some funny consequences.
>
> I don't think I know enough to be able to say if this is problematic or not.
> But I can see it having some hurdles to overcome.

More than just finals, you'd also have the problem of whether some methods have
been overridden or not. That would mean the semantic of
  d->foo();

would change depending on whether D has been fully declared or not yet.

So I agree with Tiago that this could be technically done, but it would likely
be very restricted and very fragile, to the point that I don't think it would
be accepted in the language. It's easier to just declare your member a pointer
to B and cast to D at the top of a function where you know you're going to
need it frequently (or even just once).

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2024-12-30 17:48:28