固定大小的数组矢量在运行时决定

时间:2016-12-06 08:49:53

标签: c++ arrays vector

我试图做这样的事情:

int x=128, y=256;
std::vector<std::array<float,x*y>> codes;

显然这是错的,这是正确的:

std::vector<std::array<float,128*256>> codes;

第一个问题的一个解决方案可能是使用以下宏:

#define x 128
#define y 256
...
std::vector<std::array<float,x*y>> codes;

但是我想知道在运行时是否有另一个解决方案而不是编译时。 注意没有必要使用std::array,我需要一个std::vector数组(或其他)x*y元素。

1 个答案:

答案 0 :(得分:3)

尝试使用const

const int x = 128;
const int y = 256;
...
std::vector<std::array<float,x*y>> codes;

第一个代码中的问题在于xy不是常量,因此编译器不能依赖于它们的值在运行时不会改变的事实,因此,无法在编译时确定模板参数值。