2

In some old code, I've come across functions declared like:

 void f(int (&a)[5]);

I've been told that the array is 'naked'. What does that mean?

0

2 Answers 2

2

The type int (&a)[5] means "reference (named a) to an array of 5 ints". Using that type allows you to pass an array to a function without it decaying into a pointer to its first element.

Using a reference-to-array-of-5-ints instead of a pointer-to-int as your function parameter allows you to pass an array without loss of information. Things like std::begin(a) and std::end(a) will work with a reference-to-array, but not with a pointer-to-int. On the other hand, it's impossible to use a reference-to-array with a dynamically allocated array.

1
void f(int (&a)[5]);

Means int(&a)[5] is truly a reference to an array of size 5, and cannot be passed an array of any other size.

Not the answer you're looking for? Browse other questions tagged or ask your own question.