C++ Logo

std-proposals

Advanced search

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

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Mon, 30 Jan 2023 16:31:04 +0000
On 30 January 2023 15:57:15 GMT, 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).

I don't think constexpr bit_cast for pointers can be allowed in either direction in general. Absolute pointer values (and hence their object representation) are typically not known at compile time, so they are usable in constexpr context only in limited ways.

>
>> 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
>--
>Std-Proposals mailing list
>Std-Proposals_at_[hidden]
>https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

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