什么时候将数组转换为指针? [英] When are arrays converted to pointers?
问题描述
考虑以下计算数组长度的简单示例:
Consider the following simple example computing lenght of an array:
#include <iostream>
int a[] = {1, 2, 4};
int main(){ std::cout << sizeof(a)/sizeof(a[0]) << std::endl; }
标准N4296::8.3.4/7 [dcl.array]
如果E是一个秩为i×j×的n维数组...×k,然后E出现在是受制于数组到指针转换的表达式中(4.2) 被转换为一个指向 (n-1) 维数组的指针等级 j ×...×k.
If E is an n-dimensional array of rank i×j×. . .×k, then E appearing in an expression that is subject to the array-to-pointer conversion (4.2) is converted to a pointer to an (n−1)-dimensional array with rank j ×. . .×k.
N4296::4.2/1 [conv.array]
N T 数组"或未知边界数组"类型的左值或右值of T"可以转换为指向 T 的指针"类型的纯右值.结果是指向数组第一个元素的指针.
An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be converted to a prvalue of type "pointer to T". The result is a pointer to the first element of the array.
那么作为转换主题的表达式是什么?看起来未计算的操作数不是主题.
So what is the expressions which are the subject of the convertion? Looks like unevaluated operands are not the subject.
http://coliru.stacked-crooked.com/a/36a1d02c7feff41c
推荐答案
我知道以下表达式,其中数组不会转换/衰减为指针.
I know of the following expressions in which an array is not converted/decayed to a pointer.
- 在
sizeof
运算符中使用时:sizeof(array)
- 在 addressof 运算符中使用时:
&array
- 当用于将引用绑定到数组时:
int (&ref)[3] = array;
. - 推导用于实例化模板的类型名时.
- 在
decltype
中使用时:decltype(array)
- When used in a
sizeof
operator:sizeof(array)
- When used in an addressof operator:
&array
- When used to bind a reference to an array:
int (&ref)[3] = array;
. - When deducing the typename to be used for instantiating templates.
- When used in
decltype
:decltype(array)
这篇关于什么时候将数组转换为指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!