Constexpr cast from const char* to const unsigned char* (uint8_t)

Viewed 98

My intent is to just have a pointer to an arbitrary array of bytes (uint8_t), whether that be from a C-style string (which is really just an array of bytes) or from an initializer list.

Consider the following class I am trying to get to work:

class Test {
public:
    constexpr Test(std::initializer_list<uint8_t> list):
        _size(list.size()),
        _data(std::data(list))
     {
    
     }

    constexpr Test(std::string_view str):
        _size(str.size()),
        _data(str.data())
    {

    }

    constexpr const uint8_t* data() const { return _data; }

private:
    size_t _size;
    const uint8_t* _data;
};

The problem is, the data() from std::string_view is of type const char* but uint8_t is unsigned char.

I cannot use reinterpret_cast because this is a constexpr.

What can I do to get this to work?

0 Answers
Related