Consider a std::array<T, N>
derived class with the following constexpr additions:
static constexpr auto size() { return N; }
constexpr const_reference operator[](size_t Index) const
{
if (std::is_constant_evaluated())
{
static_assert(Index < size(), "Subscript out of range.");
return data()[Index];
}
else
return std::array<T, N>::operator[](Index);
}
// Outside of class.
constexpr myArray<uint8_t, 42> Array = {};
static_assert(Array[0] == 0, "Never called");
MSVC throws an error on the first static_assert
because accessing Index
is considered a read of a variable outside its lifetime
. Given the nature of the prvalues, I can't figure out why there'd be any lifetime issues for Array[0]
. Thoughts?