在初始化变量列表中的赋值顺序是否未定义? [英] Is the order of assignment in a list of initialized variables undefined?
问题描述
如果我有以下代码,逗号是作为正常的序列点,还是行为未定义?
int i = 1,j = i;
我实际上不打算使用这个(我们的内部标准禁止甚至<$ c
它的定义很好:
8。声明:[dcl.decl]
3)声明中的每个init声明都是单独分析的,
注意:
90)带有几个声明符的声明通常相当于
对应的声明序列,每个声明都有一个
声明符。这是
T D1,D2,... Dn;
通常等价于
T D1; T D2; ... T Dn;
其中T是一个decl-specifier-seq,每个Di都是一个
init声明符。
完整性(因为注释通常 表示):
当一个名字由
引入的名称隐藏了dcl-specifiers使用的类型名时,就会发生异常,所以当同一个
dcl-说明符在后续声明中使用,它们没有
与在struct S {...}中相同的含义; S S,T;
//声明两个
结构S的实例,它不等同于
struct S {...}; S
S; S T; // error`
Possible Duplicate:
Is the comma in a variable list a sequence point?
If I have the following code does the comma act as a normal sequence point, or is the behaviour undefined?
int i = 1, j = i;
I don't actually plan to use this (our internal standard prohibits even int i, j
) but I was curious and it prooved oddly tricky to google.
It's well-defined:
8. Declarators: [dcl.decl]
3) Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.
And the note:
90) A declaration with several declarators is usually equivalent to the corresponding sequence of declarations each with a single declarator. That is
T D1, D2, ... Dn;
is usually equvalent to
T D1; T D2; ... T Dn;
where T is a decl-specifier-seq and each Di is an init-declarator.
For completness (because the note says usually):
The exception occurs when a name introduced by one of the declarators hides a type name used by the dcl-specifiers, so that when the same dcl-specifiers are used in a subsequent declaration, they do not have the same meaning, as in
struct S { ... }; S S, T;
// declare two instances of struct Swhich is not equivalent to
struct S { ... }; S S; S T; // error`
这篇关于在初始化变量列表中的赋值顺序是否未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!