0

Possible Duplicate:
Use templates to get an array's size and end address

Can someone explain this template code that gives me the size of an array? (the first answer includes obtaining the value as a compile time constant)

How to get size of an array using metaprogramming? Multidimensional would be also appreciated. So for example if I pass a type to this struct (how ever it's called, let's say get_dim) I would get:

get_dim<int>::value; //0
get_dim<int[1]>::value //1
get_dim<int[2][3]>::value //2,3
Community
  • 1
  • 1
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
  • I'm not sure what you're asking. Could you add a better description of the problem and constraints? Or maybe an example of what a solution might look like, if you had a partial idea in mind? – Karmastan Mar 07 '11 at 19:41
  • 5
    Make a little effort and **search for duplicates**. *[c++] array size* in the search box will provide multiple solutions. I could just paste the solution here and get some rep. but please just **make the slightest effort**, you have been around the site long enough to try and searching by yourself. – David Rodríguez - dribeas Mar 07 '11 at 19:45
  • 1
    @David so you're here to answer questions or to show people that they cannot search etc etc? – There is nothing we can do Mar 07 '11 at 19:57
  • 4
    @There: The FAQ says right at the top: "Please look around to see if your question has already been asked (and maybe even answered!) before you ask. " Why would you think you are exempt from this? – John Dibling Mar 07 '11 at 20:01
  • 1
    @There is nothing we can do: I provided the search criteria in the comment so that you can consider doing something similar in the future in case you did not know that you could search or that you could use tags in the search. Of course, as you said some time ago, it is much easier not to bother and just ask and get others to do the work for you... – David Rodríguez - dribeas Mar 07 '11 at 21:18
  • @David - before lecturing people about not using your search terms, why not test to make sure the items that show up in that search are relevant to the question asked? : http://stackoverflow.com/search?q=[c%2B%2B]+array+size <- I don't see a single one. – Edward Strange Mar 07 '11 at 21:26
  • The question duplicate is very similar, but the answer over there doesn't apply. – Edward Strange Mar 07 '11 at 21:29
  • I find google/bing search is better at finding results than the stackoverflow search. Just tack `site:stackoverflow.com` on the end of your search queries and BAM! Answers! – spbots Mar 07 '11 at 21:56
  • @Crazy Eddie: I don't know whether the results are not deterministic, but the third result is [In regards to array size calculation](http://stackoverflow.com/questions/5033497/in-regards-to-array-size-calculation) that is a templated version, that also links to other duplicate answers, the first of which has a very extensive [answer by litb](http://stackoverflow.com/questions/437150/can-someone-explain-this-template-code-that-gives-me-the-size-of-an-array/437178#437178) including how to obtain a compile time constant with that value. There it is, completely chewed up and easy to digest. – David Rodríguez - dribeas Mar 08 '11 at 10:37
  • @david - neither of those answer the question posted here. – Edward Strange Mar 08 '11 at 18:34
  • @Crazy Eddie: The question posted here or the last edit after the comment, and the vote to close? – David Rodríguez - dribeas Mar 09 '11 at 08:49

2 Answers2

5

For one dimensional Arrays,

template<typename T, size_t N>
size_t size_of_array(T (&)[N])
{
   return N;
}

int arr[]={1,2,2,3,4,4,4,54,5};
cout << size_of_array(arr) << endl;     

A a[] = {A(),A(), A(), A(), A()};
cout << size_of_array(a) << endl;

Output:

9
5

Full Demo at Ideone : http://ideone.com/tpWk8


EDIT:

Another way (after seeing your edit),

template<typename T>
struct get_dim;

template<typename T, size_t N>
struct get_dim<T[N]>
{
    static const int value = N;
};

int main() 
{
       cout << get_dim<int[100]>::value;
       return 0;
}

Output:

100

http://ideone.com/wdFuz


EDIT:

For two dimensional arrays:

struct size2D
{
   size_t X;
   size_t Y;
};

template<typename T, size_t M, size_t N>
size2D size_of_array(T (&)[M][N])
{
   size2D s = { M, N};
   return s;
}
int arr[][5]={ {1,2,2,5,3}, {4,4,4,54,5}} ;
size2D size = size_of_array(arr);
cout << size.X <<", "<< size.Y << endl; 

Output:

2, 5

Code : http://ideone.com/2lrUq

Nawaz
  • 353,942
  • 115
  • 666
  • 851
4

This functionality is present in Boost.TypeTraits, specifically, boost::rank<> and boost::extent<>.

If you want to know how it's done, see other answers, or the Boost source code.

John Bartholomew
  • 6,428
  • 1
  • 30
  • 39