determine array size

Started by
9 comments, last by Daivuk 20 years, 7 months ago
Fruny''s template for array_size is confusing at first, but it all becomes clear when you realize that C++ can have unnamed function parameters. I believe this is discussed in one of the first few hints in Meyer''s Essential STL book, but in a different context.

Anyway, basically array_size is a parameterized function that takes an array reference as a parameter. Just like if you were to write this function...

int doSomething (int (&array)[90]);

...it would only compile if you did something like this:

int somearray[90];
int otherarray[91];

int main ()
{
doSomething (somearray); // fine
doSomething (otherarray); // not fine
}

By the way, the parenthesis above "int (&array)[90]" are because of operator precedence, I assume.

Now onto unnamed function parameters... you actually see this all the time in C code:

int somefunction (int, int, float);

The above is a valid prototype, and... if for some reason the function has a parameter which it doesn''t use, it can safely remain anonymous during the actual function body.

As it turns out, that''s exactly what we have with array_size; we don''t care at all about what the array actually is, just its size. Therefore the parameter can (but does not have to) remain unnamed.

Anyway, assuming you understand typed and non-typed template parameters, array_size should be pretty clear to you now.

This topic is closed to new replies.

Advertisement