1

I write a code like this:

 void Print(const int & dataArray[], const int & arraySize) {  // problem 
    for(int i = 0; i<arraySize; i++) {
        cout << dataArray[i] << " ";
    }
    cout << endl;
    }

in mian() function:

`
int iArray[14] = { 7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5 }; 
int numArrays = 14;
Print(iArray, numArrays);
....
`

the compiler says that arrays of references are illegal, why it is illegal ?? I see the <effective c++>, it says recommend we use the const and reference, I just try to implement it(I'm a beginner), I also want to know in the void Print(const int dataArray[], const int & arraySize) parameter I use const, & to qualify the arraySize, is it right?(or is it much better than int arraySize or const int arraySize?), I want also use const,& to dataArray[], but I failed.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135

2 Answers2

7

An array requires its elements to be default-constructible and references are not, hence array of references are illegal. This:

const int & dataArray[]

is an array of references. If you want a reference to an array instead you need this:

const int (&dataArray)[]
K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • 3
    An array doesn't actually require its element to be default constructible. There's simply an explicit prohibition on arrays of references in 8.3.4/1. – bames53 Oct 19 '12 at 18:30
2

Pedantically, the reason why arrays of references are illegal is because the Standard explicitly forbids them.

C++03:8.3.2/4

There shall be no references to references, no arrays of references, and no pointers to references.

Emphasis mine.

One reason the Standard explicitly forbids arrays of references (maybe there are more) is because of how arrays are indexed. Suppose you do:

Gizmo& gizmos[] = {...};
Gizmo&* g3 = &gizmos[2];

There are several things wrong here. First you have a pointer to a reference, which is illegal. Second, in order to evaluate gizmos[2] the compiler must do an implicit conversion-to-pointer, and then do pointer arithmetic based on that. How big is a Gizmo&?

According to the Standard, the sizeof a reference is, itself, unspecified. However when sizeof is applied to a reference, the result is the size of the referred-to type.

C++03:5.3.3/2 Sizeof

When applied to a reference or a reference type, the result is the size of the referenced type.

Try running this code and see what happens:

#include <iostream>
#include <iomanip>
using namespace std;
struct Gizmo { int n_[100]; };

int main()
{
    typedef Gizmo& GR;
    size_t n = sizeof(GR);
    cout << n << endl;
}

When I run this on my machine, (MSVC10, Win7x64), the result is 400.

This is why arrays of references are illegal.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Wow, someone actually tried to rationalise what the Standard says, rather than just saying 'look: it's forbidden'. ;-) +1 IMO, the clearest statement of the problem with the idea of an array/container of references - kind of implicit in your answer, if not explicitly stated - is: How would we disambiguate between the address of the element vs the reference itself? We can't, without totally breaking array semantics (indexing, arithmetic) and adding pointless syntax to disambiguate. When by simply wrapping the reference in a struct, we retain both options: address of the reference or its parent. – underscore_d Jul 22 '16 at 09:48
  • ...by which I meant 'address of the _reference's target_ or its parent `struct`'. Anyway, I ramble on about all of this here - http://stackoverflow.com/questions/1164266/why-arrays-of-references-are-illegal#comment64435464_1164306 - and might eventually post it as an answer if I can get it into a useful long-form without descending into ranting, heh. – underscore_d Jul 22 '16 at 09:53