C++ allows you to declare a reference to an array. Such a reference encodes the array's dimension as part of its type so you can use reference to arrays to enforce compile-time bounds checking.
operator & takes lower
precedence than , you need to
parenthesize the ampersand in a reference-to-array declaration. For example,
the following function takes a reference to an array of 20 ints called arr:
void func( int (&arr) );
If you pass an array with the wrong size or type to func() you'll get a compilation error:
func(x); //cannot convert 'int ' to 'int (&)'
You can use references to arrays with templates too:
template <typename T> void func( T(&arr));
func(y); //OK, generating func<int>
func (d); //OK, generating func<double>
func (f); //error
Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.