I come across the following code, which returns the size of a C style array.
template <typename Type, int N>
int GetArraySize(Type (&array)[N])
{
(void) sizeof (0[array]);
return N;
}
The templated part seems to have been already explained in this question.
But still, I don't understand what is the utility of the sizeof
line. Any ideas?
Some suggest that it is to avoid unused variable warning, but a simpler #pragma
could have been used, right?
Moreover, will this piece of code be effective in any situation? Aren't there any restrictions?
I think the purpose of the line is to silent unused variable warning. The simpler would be to omit parameter name
template <typename Type, int N>
int GetArraySize(Type (&)[N])
{
return N;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With