C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::sizeof_minus_trailing_padding

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 3 Dec 2023 23:41:41 -0500
On Sun, Dec 3, 2023 at 10:14 PM Thiago Macieira via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Sunday, 3 December 2023 18:51:05 PST Jason McKesson via Std-Proposals
> wrote:
> > So if "using NUA on arbitrary types is wrong", then the feature is
> > fundamentally broken.
>
> Yes.
>
> And a kludge to work around it is that NUA is allowed to give the same address
> to two different objects but not if they are of the same type.

But that's not the problem here. And that's not even a kludge; that's
just the result of the "unique identifier rule", which predates NUA.
That rule being that you have to lay out classes and their subobjects
such that every object of type `T` must have a different address from
any other object of type `T`. This rule is part of why types that have
two base classes of the same type (inherited through different
intermediate types) aren't standard layout.

> But there's nothing that says my two unrelated empty types can't compare their
> pointer addresses for some reason after casting to void*, like say for
> inserting into an sorted container.

But that's fine; empty types are *empty*. They have nothing meaningful
to copy. There are no memcpy tricks you can do to them.

The particular problem here is with an non-empty type that wants to do
memcpy tricks that happens to be used as a NUA without its knowledge.

> Therefore, I repeat: do not use NUA on arbitrary types.

Received on 2023-12-04 04:41:57