C++ Logo


Advanced search

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

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Fri, 27 May 2022 08:45:29 -0600
On Fri, 27 May 2022 at 08:28, Oleksandr Koval <oleksandr.koval.dev_at_[hidden]>

> Why not use if constexpr?
> Because then it's not a generic code anymore, it's a workaround around an
> artificial constraint. Isn't it silly that people are forced to use bad old
> C-cast instead of C++ construction just because something is prohibited
> without a reason?

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.

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.

Received on 2022-05-27 14:45:41