C++ Logo


Advanced search

Re: Idea: extend forward declarations to include base class(s)

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sat, 22 Feb 2020 13:36:57 -0800
On Saturday, 22 February 2020 12:49:36 PST John Yates via Std-Proposals wrote:
> To me a forward declaration of either of those structs as simply struct D:
> B;
> is clearly an error. When forward declaring base classes any failure to
> match exactly the actual declaration (number, order, virtualness, etc.)
> should at the very least be UB.

That would be implied, yes.

Just to confirm without multiple or virtual inheritance:

struct B;
struct D : B;

/* later */

struct B { int i; };
struct D : B
    virtual ~D();

What is the layout of D? Two options will apply for 99% of the ABIs (64-bit

0 4 8 12 16
| i | pad | vtable |


0 4 8 12 16
| vtable | i | pad |

In the first scenario, D and B share top-of-object address. I thought the
first case would be what the IA-64 ABI would do. But a quick test at
https://gcc.godbolt.org/z/R5FLkF shows that it is in fact the second case, as
you can see from:
        movq $_ZTV1D+16, (%rax)
        movl $42, 8(%rax)

Same for the MSVC ABI.

That is, the B-in-D sub-object is not allocated at the top of the D object.

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel System Software Products

Received on 2020-02-22 15:39:39