Antoine C.
Antoine C.

Reputation: 3962

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?

Upvotes: 6

Views: 426

Answers (1)

Jarod42
Jarod42

Reputation: 218138

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;
}

Upvotes: 10

Related Questions