Count vertex array elements

Started by
10 comments, last by Brother Bob 11 years, 6 months ago

template
size_t number_of_array_elements(T (&)[N])
{
return N;
}

Could you explain, please, how your template works?
(or add an example)
Advertisement

[quote name='Brother Bob' timestamp='1349622939' post='4987693']
template
size_t number_of_array_elements(T (&)[N])
{
return N;
}

Could you explain, please, how your template works?
(or add an example)
[/quote]
I can interpret your question in two ways:

  1. you want to know how to use it, or
  2. you want to know how it works and how it is able to determine the size of an array.

I'll just go ahead and answer both, so pick your answer :)

For 1, it is just a function where you pass the array and it returns its size.

int foo[42];
int size = number_of_array_elements(foo);

The variable size is assigned the value 42, because the array foo contains 42 elements.

For 2, you need to know a bit about references to arrays. Normally when you pass an array to a function, you pass a pointer to its the first value in the array along with the size of the array. This way, the function can take any array, but since the array is decayed into a pointer, the size information about the array is also lost which is why you also have to pass its size to the function. Such a function prototype typically looks like this:

void myFunction(int *p, int n);

where p is the pointer and n is the size. The function now knows how many elements that is pointed to by p. You can pass any pointer to any array of any size to this function, but you have to manually track the size of the array.

Another way to pass an array to a function is by reference. The syntax is a little strange if you're not used to it:

void myFunction(int (&a)[42]);

The function now takes a reference to an array of 42 elements. This way, you can only pass an array of 42 elements, but the type of the array (which includes its size) is preserved entirely. In the previous function, the type of the array is partially lost: the type of the elements is implied by the pointer's type, but the array's size is lost in the pointer decay.

Now, a template is introduced to allow the type to vary so that you can pass an array of any type. Furthermore, the size of the array can also be made a template parameter. So replace the type (int in this case) and the size (42 in this case) with generic template parameters.

template<typename T, size_t N>
void myFunction(T (&a)[N]);


Next step is to ask: what do we want the function to do with the array? In this case, we don't want to do anything with the array itself, we just want the function to return the size of the array.

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


Last step to match the function I presented above is to remove the name of the parameter. Since we don't use the parameter a anywhere in the function, there is no need to give it a name so we can leave it as an unnamed parameter.

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

And there it is, the function number_of_array_elements.

This topic is closed to new replies.

Advertisement