[ACCEPTED]-Why it is impossible to create an array of references in c++?-reference

Accepted answer
Score: 29

Because indexation into an array is actually 5 defined in terms of an implicit conversion 4 to a pointer, then pointer arithmetic. So 3 to support this, you'd have to also support 2 pointers to references, and define what 1 pointer arithmetic means on them.

Score: 5

Because references aren't objects. References 4 were primarily introduced to support call 3 by reference and return by reference without 2 inserting & at call-site. What you probably 1 want is an array of pointers.

Score: 5

A reference cannot be reassigned, and has 3 no size.

If arrays of references were allowed 2 they would therefore have to be treated 1 in a special way then.

Score: 1

This is what i read at:

5.2.1 Subscripting [expr.sub]

1 A postfix expression 15 followed by an expression in square brackets 14 is a postfix expression. One of the expressions 13 shall have the type “pointer to T and 12 the other shall have enumeration or integral 11 type. The result is an lvalue of type 10 “T.” The type “T” shall be a completely-defined object 9 type.61) The expression E1[E2] is identical 8 (by definition) to *((E1)+(E2)) [ Note: see 7 5.3 and 5.7 for details of * and + and 8.3.4 6 for details of arrays. —end note ]

-C++ Draft.

int a = 10, b = 20;
int &c[] = {a, b};

So 5 imagine &c[0] would be something like *&(c+0), IMHO references 4 are like aliases. Hence going by the notion 3 of arrays it would try to dereference the 2 value held by the reference which one would 1 not want.

More Related questions