I think we need to wait til C++0x in order to really simplify that ugly function signature
Code:
template<typename T, int N>
typedef T (&array_t)[N];
** I assume that this will eventually be legal, although even Comeau with C++0x extensions doesn't recognise it right now, so perhaps I misunderstand what they actually mean by "adding support for template typedefs" :-)
Yes I would prefer an STL container too; Personally I only find myself using these to initialise containers using const arrays as lookup tables.
The reason I prefer the sizeof(char(&)[N]) approach is that the return type can be evaluated at compile time without ever actually calling the function, but I suppose a solution which actually calls a function would be OK aswell.
Code:
template<typename T, int N>
int array_size( T (&dummy_arg)[N] )
{
return N;
}
Or, if a range is needed for an STL container or algorithm then maybe returning a pointer would be better
Code:
template<typename T, int N>
T* end_of( T (&array)[N] )
{
return array+N;
}