数组初始化时,引用previous元素好吗? [英] array initialization, is referencing a previous element ok?
问题描述
const QPointF points[] =
{
QPointF(r.left() - i, r.top() - i),
QPointF(r.right() + i, r.top() - i),
QPointF(r.right() + i, r.bottom() + i),
QPointF(r.left() - i, r.bottom() + i),
points[0] // is this line valid (according to the C++ standard)?
};
虽然这编译与MS Visual Studio的编译器,我不知道这是否是根据C ++标准有效code。
While this compiles with the MS Visual Studio Compiler, i am not sure if this is valid code according to the C++ Standard.
从标准行情将高AP preciated。
推荐答案
<子> C ++ 03 / C ++ 11的答案
没有,不是这样的。
在确实存在 1 的 =
,点的右侧但其所有的操作数已被评估后初始化器只适用。
On the right-hand side of the =
, points
does exist1 but the initialiser is only applied after all its operands have been evaluated.
-
如果
点
是在命名空间内(因此具有静态存储时间,并已初始化为零 2 ),那么这个是安全的,但你的点使用[0]
有要给你0
,而不是QPointF(r.left() - 我,r.top() - I)。
再次
If
points
is at namespace scope (and thus has static storage duration and has been zero-initialized2), then this is "safe" but your use ofpoints[0]
there is going to give you0
, rather thanQPointF(r.left() - i, r.top() - i)
again.
如果点
具有自动存储持续时间MDASH;它尚未初始化,所以你使用点[0]
正在尝试使用未初始化变量,其中点[0]
有一个不确定的值......这是坏 3 。
If points
has automatic storage duration — it has not yet been initialised so your use of points[0]
is attempting to use an uninitialised variable, where points[0]
has an indeterminate value... which is bad3.
这很难提供这个参考标准,比其他地说,在没什么8.5的初始化
,明确使这成为可能,并且规则其他填写其余部分。
It's difficult to provide standard references for this, other than to say that there is nothing in 8.5 "Initializers"
that explicitly makes this possible, and rules elsewhere fill in the rest.
1 [n3290:3.3.2 / 1]:
申报为名称的点是立即其完整的声明符后(第8条)和它的初始化之前(如果有的话),除如下所述。 [示例:的
1
[n3290: 3.3.2/1]:
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
与自己的(不确定)值初始化。的末端例如的]
Here the second x
is initialized with its own (indeterminate) value. —end example ]
2 [n3290:3.6.2 / 2]:
与静态存储时间(3.7.1)或线程存储持续时间的变量(3.7 0.2)应是零初始化(8.5)
任何其它初始化之前发生。 [..]
2 [n3290: 3.6.2/2]:
Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5)
before any other initialization takes place. [..]
3 [n3290:17.6.3.3/2]:
[..] [注:的操作
涉及不定值可能会导致不确定的行为。的末端笔记的]
3 [n3290: 17.6.3.3/2]:
[..] [ Note: Operations
involving indeterminate values may cause undefined behavior. —end note ]
这篇关于数组初始化时,引用previous元素好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!