C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Allow reinterpret_cast in constexpr when C-cast is allowed

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 27 May 2022 11:42:15 -0400
On Fri, May 27, 2022 at 11:05 AM Oleksandr Koval via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>>
>> The C-style cast and reinterpret_cast are not the same. A C-style cast can perform a static_cast and const_cast; a reinterpret_cast can perform the identity conversion but after that performs conversions that cannot appear in constant evaluation.
>
> C-cast has multiple stages and some of them are allowed in constexpr context. reinterpret_cast also has multiple stages but none of them are allowed in constexpr, what's the logic here? Why is the same conversion allowed with one syntax and not with the other?
>
>> If your argument is that reinterpret_cast as identity conversion should be allowed to appear in constant evaluation, that might make sense, but it's hard to see that as much of an improvement over being explicit with if constexpr. That is, it's beneficial reading the code to see when it will perform the weaker, safer operation.
>
> Not only identity but all conversions that are allowed for C-cast in constexpr context.

`reinterpret_cast` is not an alternative spelling of C-style casts,
and we should not make them behave as such.

Received on 2022-05-27 15:43:54