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).