C++ Logo

std-proposals

Advanced search

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

From: Oleksandr Koval <oleksandr.koval.dev_at_[hidden]>
Date: Fri, 27 May 2022 16:59:11 +0300
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
    }
};

-- 
Regards,
Oleksandr Koval.

Received on 2022-05-27 13:59:28