相关疑难解决方法(0)

为什么非const std :: array :: operator []不是constexpr?

我正在尝试使用给定的函数在编译时填充2D数组.这是我的代码:

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
Run Code Online (Sandbox Code Playgroud)

它运行得很好,table.data在编译时正确填充.

然而,如果我改变普通2D阵列int[H][W]std::array<std::array<int, H>, W> …

c++ arrays constexpr c++14

30
推荐指数
3
解决办法
2579
查看次数

标签 统计

arrays ×1

c++ ×1

c++14 ×1

constexpr ×1