C++ Logo

std-proposals

Advanced search

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

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Mon, 30 Jan 2023 16:57:15 +0100
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).

> Thanks,
> --
> Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
> KDAB (France) S.A.S., a KDAB Group company
> Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
> KDAB - The Qt, C++ and OpenGL Experts
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-01-30 15:57:28