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

I come across the following code that 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 template part seems to have already been explained in this question .


But still I do not understand what the line utility is sizeof. Any ideas? Some suggest that you should avoid warning an unused variable, but a simpler #pragmaone can be used , right?

Also, will this piece of code be effective in any situation? Are there any restrictions?

+4
source share
1 answer

I think the goal of the line is to silently warn an unused variable. It would be simpler to omit the parameter name

template <typename Type, int N>
int GetArraySize(Type (&)[N])
{
    return N;
}
+10

Source: https://habr.com/ru/post/1679609/


All Articles