Why is the size of an array passed to a function by reference known to the compiler in C++?

Viewed 320

I know that when I want to pass an array to a function, it will decay into pointer, so its size won't be known and these two declarations are equivalent:

void funtion(int *tab, int size);

and

void funtion(int tab[], int size);

And I understand why. However, I checked that when I pass an array as a reference:

void funtion(int (&tab)[4]);

the compiler will know the size of the array and won't let me pass an array of different size as an argument of this function.

Why is that? I know that when I pass an array by address, the size isn't taken into account while computing the position of the ith element in the array, so it is discarded even if I explicitly include it in the function declaration:

void funtion(int tab[4], int size);

But what is different when I pass an array by reference? Why is its size known to the compiler?

Note: I'm interested in arrays whose size is known at compile time, so I didn't use any templates.

I found a similar question on Stack Overflow, however it doesn't answer my question - it doesn't explain why the compiler knows the size of the array, there is just some information on how to pass arrays to functions.

3 Answers
Related