ylliX - Online Advertising Network
Vertex AI - Antrophic and Mistral models: Why does it require Imegen access?

How to use std::array’s data and other member functions in a constexpr context?


I defined the following class, expecting to be able to use std::array in a constexpr context, but calling the data function results in a compilation error.

#include <array>

template <size_t N>
struct ConstexprString
{
    consteval ConstexprString(const char (&_str)[N])
        : char_array(std::to_array(_str))
    {
    }

    consteval auto Data() const -> const char*
    {
        return char_array.data();
    }

private:
    const std::array<char, N> char_array;
};

Test code

constexpr sz::tp::detail::ConstexprString csexpr_str("123\\0");
constexpr auto d = csexpr_str.Data();  // error C2131: expression did not evaluate to a constant



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *