A classic templating problem. Here's a simple solution like how the C++ standard library does. The basic idea is to have a recursive template that will count one by one each dimension, with a base case of 0 for any type that is not a vector.
#include <vector>
#include <type_traits>
template<typename T>
struct dimensions : std::integral_constant<std::size_t, 0> {};
template<typename T>
struct dimensions<std::vector<T>> : std::integral_constant<std::size_t, 1 + dimensions<T>::value> {};
template<typename T>
inline constexpr std::size_t dimensions_v = dimensions<T>::value; // (C++17)
So then you could use it like so:
dimensions<vector<vector<vector<int>>>>::value; // 3
// OR
dimensions_v<vector<vector<vector<int>>>>; // also 3 (C++17)
Edit:
Ok, I've finished the general implementation for any container type. Note that I defined a container type as anything that has a well-formed iterator type as per the expression begin(t)
where std::begin
is imported for ADL lookup and t
is an lvalue of type T
.
Here's my code along with comments to explain why stuff works and the test cases I used. Note, this requires C++17 to compile.
#include <iostream>
#include <vector>
#include <array>
#include <type_traits>
using std::begin; // import std::begin for handling C-style array with the same ADL idiom as the other types
// decide whether T is a container type - i define this as anything that has a well formed begin iterator type.
// we return true/false to determing if T is a container type.
// we use the type conversion ability of nullptr to std::nullptr_t or void* (prefers std::nullptr_t overload if it exists).
// use SFINAE to conditionally enable the std::nullptr_t overload.
// these types might not have a default constructor, so return a pointer to it.
// base case returns void* which we decay to void to represent not a container.
template<typename T>
void *_iter_elem(void*) { return nullptr; }
template<typename T>
typename std::iterator_traits<decltype(begin(*(T*)nullptr))>::value_type *_iter_elem(std::nullptr_t) { return nullptr; }
// this is just a convenience wrapper to make the above user friendly
template<typename T>
struct container_stuff
{
typedef std::remove_pointer_t<decltype(_iter_elem<T>(nullptr))> elem_t; // the element type if T is a container, otherwise void
static inline constexpr bool is_container = !std::is_same_v<elem_t, void>; // true iff T is a container
};
// and our old dimension counting logic (now uses std:nullptr_t SFINAE logic)
template<typename T>
constexpr std::size_t _dimensions(void*) { return 0; }
template<typename T, std::enable_if_t<container_stuff<T>::is_container, int> = 0>
constexpr std::size_t _dimensions(std::nullptr_t) { return 1 + _dimensions<typename container_stuff<T>::elem_t>(nullptr); }
// and our nice little alias
template<typename T>
inline constexpr std::size_t dimensions_v = _dimensions<T>(nullptr);
int main()
{
std::cout << container_stuff<int>::is_container << '\n'; // false
std::cout << container_stuff<int[6]>::is_container<< '\n'; // true
std::cout << container_stuff<std::vector<int>>::is_container << '\n'; // true
std::cout << container_stuff<std::array<int, 3>>::is_container << '\n'; // true
std::cout << dimensions_v<std::vector<std::array<std::vector<int>, 2>>>; // 3
}
std::vector
is a run-time thing, not a compile-time one. If you want a compile-time size container, look tostd::array
. Also; remember thatconstexpr
only means "may be evaluated at compile time" - there's no promise that it will be. It may be evaluated at run-time. – Howdoyoudostd::vector
s are nested within each other. For example withstd::vector<std::vector<int>> v;
,GetDepth(v);
would return 2 since it is a 2 dimensional vector. The size is irrelevant. – Jeffreysvector
isn't always the best way to do things. Manual 2d or 3d indexing of a single flat vector can be more efficient, depending on the use-case. (Just integer math instead of pointer-chasing from the outer levels.) – Snowmobilestd::vector
, but an always-rectangular array isn't one of them. (Especially if it doesn't need to be resizable in any dimension, but even then it's not great.) Use-cases do include a sparse or ragged array. – Snowmobilerank
for this query on array types (in agreement with the mathematical nomenclature for tensors). Perhaps that is a better word here than "depth". – Gnawconstexpr
as part of a template argument then by definition it MUST be evaluated at compile time otherwise the template as a whole will fail to compile. Whether aconstexpr
will be evaluated at compile time or runtime is not as unpredictable as your comment makes it seem. – Jeffreys