I have an N-dimensional Matrix class that has a constructor with a parameter pack. Is it possible to set the size of the std::array
member variable depending on the values in the parameter pack? As far as I understand the values in the parameter pack should be known at compile time.
template<size_t N>
class Matrix {
public:
template<typename... Exts>
Matrix(Exts... exts) : dimSizes{exts...} { }
private:
std::array<size_t, N> dimSizes;
std::array<float, N> data;
// e.g something like this: std::array<float, dimSizes[0]> data;
};
int main(void) {
Matrix<3> mat(2, 3, 2);
return 0;
}
Is it possible to set the size of the
std::array
member variable depending on the values in the parameter pack?// e.g something like this:
std::array<float, dimSizes[0]> data;
No, as far I know is impossible exactly as you want.
Because, this way, different instances of the same class would contain members with same name but different types. Strictly forbidden in a strongly typed language as C++.
If you want a std::array
with different size, you have to differentiate the types; so the dimension for the second std::array
has to be a template parameter.
Obviously you can substitute the std::array
with a container that doesn't depend from the size; as suggested by Piotr Skotnicki a possible solution is std::vector