I'm trying to devise a way for the compiler to deduce the size of an array passed as a non-type template parameter.
I can make the template work if I explicitly pass the array size as a third template parameter, but that opens the technique to errors.
Can anyone think of a way of doing this. The code below doesn't compile, but it gives an idea of what I'm trying to achieve.
// Compile time deduction of array size.
template <typename T, const size_t SIZE>
char(&array_size(T(&array)[SIZE]))[SIZE];
#define ARRAY_SIZE(x) (sizeof(array_size(x)))
template <typename T, T BEGIN[]>
struct Test
{
enum
{
SIZE = ARRAY_SIZE(BEGIN)
};
};
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int main()
{
Test<int, a> test;
return 0;
}
[EDIT] I forgot to point out that the solution must also be C++03 compatible.