Is there a way to use enum elements to index into an array in a way that minimizes indexing errors.
Example:
enum class Index : int {
Foo, Bar, Koo
};
static const int Values[] = { 42, 7, 3 };
constexpr int GetValue(Index i) {
return Values[int(i)];
}
auto v = GetValue(Index::Foo);
The problem with the above is that modifying either Index or Values may cause indexes to point to wrong values, and such errors would not be detected by the compiler.