Why it is impossible to create an array of references in c++?

后端 未结 4 1540
庸人自扰
庸人自扰 2020-12-25 12:08

C++ Standard 8.3.2/4 says:

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

4条回答
  •  再見小時候
    2020-12-25 12:18

    This is what i read at:

    5.2.1 Subscripting [expr.sub]

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

    -C++ Draft.

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

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

提交回复
热议问题