Khoguan Phuann wrote:
> In this declaration and definition:
> int a = 2, b = a;
> Is it guaranteed that b will always be initialized as 2 ?
> If yes, then can we say that a = 2 is always analysed(or
> evaluated?) before b = a ?
Yes. Strictly stated, the observable behavior of the program
must be as if all of the side effects of the 'a = 2' part of the
declaration took place before the evaluation of the 'b = a' part
starts. (In practice, of course, in this simple example, a
compiler could assign 2 to both a and b in any order, or even in
parallel, because doing so would result in the same observable
> K&R II, Sec. 3.5, p.63 says:
> "The commas that separate function arguments, variables in
> declarations, etc., are not comma operators, and do not
> guarantee left to right evaluation."
True to a point. The commas here are definitly not operators
(and an overloaded comma operation will never be used for
them). The point here is that each declaration is a separate
complete expression, and that there are sequence points at the
end of a complete expression.
> But the C++ standard Clause 8, paragraph 3 says "Each
> init-declarator in a declaration is analyzed separately as if
> it was in a declaration by itself." without mentioning the
> order of their being analyzed.
> But its footnote(#85) does say: "A declaration with several
> declarators is usually equivalent to the corresponding
> sequence of declarations each with a single declarator. That
> 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 a
> So can I rewrite my example as
> int a = 2;
> int b = a;
> and be assured that b will be correctly initialzed? How about
> int a = 0;
> int b = ++a, c = ++a; // equals " int b = ++a; int c = ++a; "?
Also no problem.
> Does K&R II's C rule above still apply in C++?
The K&R rule that you quote is only part of the picture; I
suspect that if you take it in context, it is speaking more or
less about the general distinction between comma operators and
comma punctuators. The comma punctuator does not, in itself,
generate a sequence point or guarantee any ordering. In this
particular case, however, it does separate the declarator list
into separate declarators; each declarator contains a complete
expression, and the declarators are evaluated in order.
James Kanze GABI Software
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]