C++中数组索引的类型 [英] Type of array index in C++

查看:58
本文介绍了C++中数组索引的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++编程语言中数组索引的类型是什么?例如在这样的声明中:

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

int tab[5];

5 转换成什么类型​​?或者它只是简单的 int?

To what type 5 is converted? or maybe is it just plain int?

推荐答案

在那段代码中,5 只是一个普通的整数字面量,所以这里只是一个普通的 int.

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) 指定数组声明符:

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

在声明 T D 中,其中 D 具有以下形式

In a declaration T D where D has the form

D1 [ constant-expressionopt ] attribute-specifier-seqopt

且声明T D1中的标识符类型为衍生声明符类型列表T",则D的标识符类型为数组类型;如果 D 的标识符类型包含自动类型说明符,则程序格式错误.T称为数组元素类型;此类型不应是引用类型、(可能有 cv 限定的)类型 void、函数类型或抽象类类型.如果常量表达式 (5.19) 存在,它应该是一个整数常量表达式并且它的值应该大于零.

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 下标描述了表达式中括号中的内容:

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

后缀表达式后跟方括号中的表达式是后缀表达式.其中一个表达式应具有指向 T 的指针"的类型,另一个应具有无作用域枚举或整数类型.结果是T"类型的左值.类型T"应该是一个完全定义的对象类型.表达方式E1[E2] 与 *((E1)+(E2)) 相同(根据定义)

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))

这篇关于C++中数组索引的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆