Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (void) sizeof (0[array]) mean?

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 #pragmacould have been used, right?

Moreover, will this piece of code be effective in any situation? Aren't there any restrictions?

like image 204
Antoine C. Avatar asked Dec 23 '22 17:12

Antoine C.


1 Answers

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;
}
like image 108
Jarod42 Avatar answered Jan 03 '23 10:01

Jarod42