I'm trying to fill a 2D array on compile time with a given function. Here is my 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
It works just fine, table.data
is properly populated at compile time.
However, if I change plain 2D array int[H][W]
with std::array<std::array<int, H>, W>
, I have an error in the loop body:
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
Obviously, I'm trying to call non-const overload of std::array::operator[]
, which is not constexpr
. The question is, why it is not constexpr
? If C++14 allows us to modify variables declared in constexpr
scope, why this is not supported by std::array
?
I used to think that std::array
is just like plain array, only better. But here is an example, where I can use plain array, but cannot use std::array
.