Is the order of assignment in a list of initialized variables undefined? [duplicate]

岁酱吖の 提交于 2019-12-10 17:07:39

问题


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.


回答1:


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 tostruct S { ... }; S S; S T; // error`



来源:https://stackoverflow.com/questions/12729962/is-the-order-of-assignment-in-a-list-of-initialized-variables-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!