Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of elements in std::array<T, N> without having to create its instance?

There is std::array<T, N>::size(), but it's non-static, so it requires an instance of std::array. Is there a way to get the value it returns (which is the N of std::array<T, N>) without having to construct an instance of the array? For a normal array, I could have used sizeof, but I see no guarantee that sizeof(std::array<T, N>) == N * sizeof(T) be true.

like image 261
dragonroot Avatar asked Mar 13 '20 18:03

dragonroot


1 Answers

There's std::tuple_size<std::array>.

static_assert(std::tuple_size<std::array<int, 5>>::value == 5);
like image 131
David G Avatar answered Oct 19 '22 11:10

David G