I have an std::vector defined as:
std::vector<glm::vec3> faces;
And I want to use the size of that vector as the length of an array of floats. Right now I’m trying to do it like this:
float vertices[faces.size()][3];
But I keep getting errors saying that you must use a constant value. I thought maybe it was because the size of the vector can change, so I tried this instead:
const int size = faces.size();
float vertices[size][3];
But I still get the same error. Is it possible to do this?
Why do you want to use a C array anyway? A
vectorwould be much more apt here.And if you already know the size of the inner vector and its size is fixed, it’s a safe bet that a struct would be better suited than a vector here:
The usefulness of C-style vectors is severely limited in C++. The only use is basically if you know their size (and usually also the contents) at compile-time and use them as a glorified constant. For everything else, there exist better containers. And even this niche is filled in the next standard by
std::array.