Why is non-const std::array::operator[] not constexpr?

Ok, it is indeed an oversight in the standard. There even exists a proposal to fix this: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0107r0.pdf

[N3598] removed the implicit marking of constexpr member functions as const. However, the
member functions of std::array were not revisited after this change, leading to a surprising lack
of support for constexpr in std::array’s interface. This paper fixes this omission by adding
constexpr to the member functions of std::array that can support it with a minimal amount of
work.

UPD: Fixed in C++17: https://en.cppreference.com/w/cpp/container/array/operator_at

Leave a Comment