#include <array> template <typename E, class T, E N> class enum_array : public std::array<T, static_cast<std::size_t>(N)> { public: T& operator[](E e) { return std::array<T, static_cast<std::size_t>(N)>::operator[]( static_cast<std::size_t>(e)); } const T& operator[](E e) const { return std::array<T, static_cast<std::size_t>(N)>::operator[]( static_cast<std::size_t>(e)); } }; enum class ENUM { a, b, c, last }; int main() { enum_array<ENUM, int, ENUM::last> t; return t.size(); }
How to use a C++ enum class enumerator as std::array index without an explicit cast Archive du 25/12/2017 le 03/02/2023