# Any way to find out how many "dimensions" a vector has?

• 04-15-2008
6tr6tr
Any way to find out how many "dimensions" a vector has?
Is there a way at runtime to find out how many dimensions a vector has? For example, vector<vector<int> > would be a two-dimensional vector (so this is diff. from its size() ). Is there any way to discover that at runtime?
• 04-15-2008
brewbuck
Quote:

Originally Posted by 6tr6tr
Is there a way at runtime to find out how many dimensions a vector has? For example, vector<vector<int> > would be a two-dimensional vector (so this is diff. from its size() ). Is there any way to discover that at runtime?

The drill_down function I posted earlier is easily adapted to do this.

Code:

```template <typename T> int vector_dimensions(const T &) {     return 0; } template <typename T> int vector_dimensions(const vector<T> &) {     return 1 + vector_dimensions(typename vector<T>::value_type); }```
Note that the parameters don't even have names, since they are not used for anything.
• 04-15-2008
6tr6tr
Quote:

Originally Posted by brewbuck
The drill_down function I posted earlier is easily adapted to do this.

Code:

```template <typename T> int vector_dimensions(const T &) {     return 0; } template <typename T> int vector_dimensions(const vector<T> &) {     return 1 + vector_dimensions(typename vector<T>::value_type); }```
Note that the parameters don't even have names, since they are not used for anything.

Thanks! I was doing something similar to that since you'd posted your code but then I thought I should check if there was a "standard" way/function for this.
• 04-15-2008
brewbuck
Quote:

Originally Posted by 6tr6tr
Thanks! I was doing something similar to that since you'd posted your code but then I thought I should check if there was a "standard" way/function for this.

I just realized my code has a bug in it. You do need a named parameter:

Code:

```template <typename T> int vector_dimensions(const vector<T> &vec_p) {     return 1 + vector_dimensions(vec_p[0]); }```
This silliness could be avoided by writing it as a proper metaprogram instead.
• 04-15-2008
6tr6tr
Quote:

Originally Posted by brewbuck
I just realized my code has a bug in it. You do need a named parameter:

Code:

```template <typename T> int vector_dimensions(const vector<T> &vec_p) {     return 1 + vector_dimensions(vec_p[0]); }```
This silliness could be avoided by writing it as a proper metaprogram instead.

Thanks, can you explain a bit more what that would entail?
• 04-15-2008
brewbuck
Quote:

Originally Posted by 6tr6tr
Thanks, can you explain a bit more what that would entail?

Well, doing this with functions forces you to pass in a specific instance, when it isn't really necessary (actually, it's not necessary for the function version either, but you'd have to specify the template parameter explicitly instead of letting the compiler deduce it from the argument). A metaprogram would be written with class templates instead of function templates:

Code:

```template <typename T> struct vector_dimensions {     static const int n = 0; }; template <typename T> struct vector_dimensions<vector<T> > {     static const int n = 1 + vector_dimensions<T>::n; };```
This is superior to the function version in a few ways. First, it no longer requires you to pass an instance. You just refer to vector_dimensions<some_type>::n. Second, it is more likely that the compiler will be able to optimize this completely statically, at compile time. With the function implementation the possibility remains that it all happens at runtime, which would be inefficient and unnecessary.

The only downside is having to specify the type explicitly even if you DO have an object at hand. That can be addressed by pairing the vector_dimensions<> template with a small helper function:

Code:

```template <typename T> int get_vector_dimensions(const T &vec_p) {     return vector_dimensions<T>::n; }```
So if all you have is a type, you can get the dimensions through vector_dimensions<T>::n. If you have an actual instance, call it x, you can just say get_vector_dimensions(x).
• 04-15-2008
6tr6tr
Quote:

Originally Posted by brewbuck
So if all you have is a type, you can get the dimensions through vector_dimensions<T>::n. If you have an actual instance, call it x, you can just say get_vector_dimensions(x).

WOW. That is REALLY cool! Thanks!!