Use template to measure size of array

At first, I wrote something like this
1
2
template<typename T, size_t size>
inline size_t const sizeof_array(T const [size]){return size;}


but it failed, then I find a solution form the other place, like this.

1
2
template<typename T, size_t size>
inline size_t const sizeof_array(T const (&)[size]){return size;}


what is the meaning of (&)? Why version one fail?
Thanks a lot
1
2
template<typename T, size_t size>
inline size_t const sizeof_array(T const (&varname)[size]){return size;}


I put in varname to make it easier to read. It is still the same protoype function. Basically it says the argument should be a variable that is an array of length size and the elements inside the array is of type T.

But I am still a bit confused why need the const keyword. I would presume it says inside the function sizeof_array, the variable varname cannot be made to point to other memory location ?
Last edited on
Topic archived. No new replies allowed.