C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A type trait to detect if value initialization can be achieved by zero-filling

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Mon, 30 Jan 2023 16:25:17 +0000
On Mon, 30 Jan 2023 at 15:57, Marcin Jaczewski via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> pon., 30 sty 2023 o 13:07 Giuseppe D'Angelo via Std-Proposals
> <std-proposals_at_[hidden]> napisaƂ(a):
> >
> > Hello,
> >
> > Il 30/01/23 13:04, Marcin Jaczewski via Std-Proposals ha scritto:
> > > T z = {};
> > > std::array<char, sizeof(z)> a = {};
> > > return std::bit_cast<decltype(a)>(z) == a;
> > > }
> > > ```
> > >
> > > It looks like it works for GCC and Clang,
> > > it needs some guards to allow only simple types (no e.g.
> `std::string`),
> > > but the core of this functionality is already available.
> >
> > It's already in the paper:
> >
> > https://isocpp.org/files/papers/D2782R0.html#usercode
> >
> > TL;DR: doesn't work in the general case. You can't bit_cast pointers
> > during constant evaluation, and it's too strict on the requirements on
> `T`.
> >
>
> Right, you can't bit-cast pointers, in theory we could relax from
> pointer conversion
> but it would make cast not symmetric
> (as in other way conversion would be asking for UB in the compiler).
>

>From pointer to integer isn't feasible, except for null pointers and
pointers to member (functions and static-lifetime objects don't have actual
addresses until link time or potentially load time in the case of DSOs and
ASLR). Integer to pointer is feasible in those cases (null pointers and
pointer to member), but would allow bypassing access checks for non-null
pointers to member, so that should probably be blocked. Pointer to pointer
is interesting - clearly pointer to similar pointer (e.g. add/remove cv) is
fine; object pointer to/from void* should probably be allowed as and when
the cast between those types is allowed.

As Giuseppe wrote, I'm drafting a paper on this, but looking for motivation
(more than "wouldn't it be cool if").

Received on 2023-01-30 16:25:31