C++ Logo

std-discussion

Advanced search

Re: I need strict aliasing for simple types

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 19 Sep 2021 22:55:03 -0400
On Sun, Sep 19, 2021 at 2:14 PM Andy via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> For example, a programmer has to write small tables (<64K) of bytes for zip or chess program. In future (or depend of efficiency tests) table elems can change to short or int, or index can change from ushort to int.
> Is well not to special defined alises like IndexType and ElemType.
> But is problem, if types is not strict and badly in some places is used other alias.
> In C++ is possible strict aliasing ?

I think you're using terminology incorrectly.

You have strict aliasing for *all types*, "simple" or not. The strict
aliasing rule basically prevents you from just pretending objects
exist in a piece of memory. If there's an `int` at an address, then
that's what is there.

So it seems like you want to *prevent* strict aliasing for "simple
types". And no, you can't just do that. You can use a `union` or a
`variant`, but you can't arbitrarily shift between the two sets of
types.

You can reuse memory for a new type by just using placement-new over
an array of storage.

Received on 2021-09-19 21:55:17