C++ Logo

std-proposals

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:16:20 -0600
On Fri, 27 May 2022 at 07:59, Oleksandr Koval via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> That is, in:
>
> template<typename To, typename From>
> constexpr To cast(From p){
> return (To)p; // #1
> // return reinterpret_cast<To>(p); // #2
> }
>
> line #2 should work in constexpr context in all cases where #1 works, in
> other cases it should be not an error but just a non-constexpr function.
> Note that I don't propose to silently make the function non-constexpr when
> it contains reinterpret_cast, this should be allowed only in generic
> context, when types are not known.
>
> reinterpret_cast is generally preferred over C-cast but in some generic
> code reinterpret_cast doesn’t work. If you wonder about use-case, I have an
> array-like wrapper around memory buffer. Type of element is not always the
> same as the type of a buffer's byte. I want to mark my functions
> `constexpr` and have them actually `constexpr` only when types match.
> Right now I’m forced to use C-cast just because reinterpret_cast is
> unconditionally forbidden even when it does no harm:
>
> template<typename Byte, typename T>
> class ArrayPtr{
> Byte* ptr;
>
> constexpr T* data() const noexcept{
> return (T*)ptr;
> // return reinterpret_cast<T*>(ptr); // not allowed
> }
> };
>

Why not use if constexpr?

    constexpr T* data() const noexcept{
        if constexpr (std::is_constructible_v<T*, Byte*>)
            return static_cast<T*>(ptr);
        else
            return reinterpret_cast<T*>(ptr);
    }

A constexpr function is allowed to contain reinterpret_cast off the
constant-evaluated path.

Received on 2022-05-27 14:16:32