Type of array index in C++

后端 未结 4 1341
甜味超标
甜味超标 2020-12-06 16:35

What is the type of array index in C++ programming language? For example in such statement:

int tab[5];

To what type 5 is converted? or may

4条回答
  •  情书的邮戳
    2020-12-06 16:56

    In that code, 5 is just a plain integer literal, so it's just a plain int here.

    §8.3.4 Arrays in n3290 (~ C++11) specifies array declarators:

    In a declaration T D where D has the form

    D1 [ constant-expressionopt ] attribute-specifier-seqopt
    

    and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is an array type; if the type of the identifier of D contains the auto type-specifier, the program is ill-formed. T is called the array element type; this type shall not be a reference type, the (possibly cv-qualified) type void, a function type or an abstract class type. If the constant-expression (5.19) is present,it shall be an integral constant expression and its value shall be greater than zero.

    §5.2.1 Subscripting describes what can go in the brackets in expressions:

    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 unscoped enumeration or integral type. The result is an lvalue of type “T.” The type “T” shall be a completely-defined object type. The expression E1[E2] is identical (by definition) to *((E1)+(E2))

提交回复
热议问题