可以(C / C ++)数组初始化引用本身吗? [英] Can a (C/C++) array initialization reference itself?

查看:127
本文介绍了可以(C / C ++)数组初始化引用本身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道下列形式的初始化:

I was wondering about an initialization of the following form:

int  array[] = {
v - 1,
array[0] + 1
} ;

在初始化第二个元素时,使用第一个元素的值,但整个数组是尚未初始化。这发生在使用g ++编译,但是我不确定这是否实际上是可移植的和一个很好定义的结构。

In the initialization of the second element, the value of the first is used, but the entire array is not yet initialized. This happens to compile with g++, but I was unsure whether this is actually portable and a well defined construct?

推荐答案

2声明点:


名称的声明点紧接在其完整声明符(第8条)之后,在
初始化程序(如果有),除非另有说明。 [示例:

The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer (if any), except as noted below. [ Example:



int x = 12;
{ int x = x; }




这里,第二个x被初始化为它自己的。 -end example]

Here the second x is initialized with its own (indeterminate) value. —end example ]

所以你是指正确的数组,它的名字在 =

So you are referring to the array correctly, its name is known after the =.

然后,8.5.1汇总:

Then, 8.5.1 Aggregates:


一个聚合是数组或类[...]

An aggregate is an array or a class [...]

17:初始化子句中的完整表达式按照它们出现的顺序进行计算。

17: The full-expressions in an initializer-clause are evaluated in the order in which they appear.

但是,我没有看到评估值实际写入数组的引用,因此我不会依赖于此,甚至会声明你的代码没有被很好地定义。

However, I see no reference to when the evaluated values are actually written into the array, so I wouldn't rely on this and would even go so far to declare your code as not well defined.

这篇关于可以(C / C ++)数组初始化引用本身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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