I m trying to create a vector with not known number of dimensions at compile time.
i found here some topics talking on the same subject but didn't answered my case like Create n-dimensional vector with given sizes and C++: generate multidimensional vector of unknown depth
i could extract one working solution (compiles without errors) from first link above but i couldn't implement it.
template <typename T, int n>
struct NDVector {
typedef std::vector<typename NDVector<T, n - 1>::type> type;
};
template <typename T>
struct NDVector<T, 0> {
typedef T type;
};
template <typename T>
std::vector<T> make_vector_(std::size_t size) {
return std::vector<T>(size);
}
template <typename T, typename... Args>
typename NDVector<T, sizeof...(Args) + 1>::type make_vector_(std::size_t first, Args... sizes) {
typedef typename NDVector<T, sizeof...(Args) + 1>::type Result;
return Result(first, make_vector_<T>(sizes...));
}
and create my vector variable like below
NDVector<int,4> myVector;
This is the only reasonable solution, but i couldn't push, erase, clear .... from myVector. also cannot use sub-scripting notation (myVector[x]). the second problem here that i couldn't use variable int instead of a constant number in the declaration (change number '4' with variable).
My goal is to be able to declare the vector like that:
unsigned n;
NDVector<int,n> myVector;
and also being able to use the 'myVector' variable like normal vectors