Warum ist non-const std :: array :: operator [] nicht constexpr?

Ich versuche, ein 2D-Array zur Kompilierzeit mit einer bestimmten Funktion zu füllen. Hier ist mein Code:

template<int H, int W>
struct Table
{
  int data[H][W];
  //std::array<std::array<int, H>, W> data;  // This does not work

  constexpr Table() : data{}
  {
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        data[i][j] = i * 10 + j;  // This does not work with std::array
  }
};

constexpr Table<3, 5> table;  // I have table.data properly populated at compile time

Es funktioniert gut,table.data wird zur Kompilierungszeit korrekt ausgefüllt.

Allerdings, wenn ich einfach 2D-Array ändernint[H][W] mitstd::array<std::array<int, H>, W>, Ich habe einen Fehler im Schleifenkörper:

error: call to non-constexpr function 'std::array<_Tp, _Nm>::value_type& std::array<_Tp, _Nm>::operator[](std::array<_Tp, _Nm>::size_type) [with _Tp = int; long unsigned int _Nm = 3ul; std::array<_Tp, _Nm>::reference = int&; std::array<_Tp, _Nm>::value_type = int; std::array<_Tp, _Nm>::size_type = long unsigned int]'
data[i][j] = i * 10 + j;
^
Compilation failed

ffensichtlich versuche ich, eine nicht konstante Überladung von @ aufzurufestd::array::operator[], das ist nichtconstexpr. Die Frage ist, warum es nichtconstexpr? Wenn wir in C ++ 14 Variablen ändern können, die in @ deklariert siconstexpr scope, warum dies nicht von @ unterstützt wistd::array?

Ich habe gedacht, dassstd::array ist wie ein einfaches Array, nur besser. Aber hier ist ein Beispiel, wo ich einfaches Array verwenden kann, aber nicht @ verwenden kastd::array.

Antworten auf die Frage(8)

Ihre Antwort auf die Frage