Topic: order of initializers in declarations?


Author: warwick@cs.uq.oz.au (Warwick Allison)
Date: 24 Mar 93 05:40:47 GMT
Raw View
pkt@lpi.liant.com (Scott Turner) writes:
>pjl@cs.uiuc.edu (Paul Lucas) writes:

>>  Is the order of initializations in declarations defined?

>It's defined in C by means of sequence points.

BEWARE.  For the code fragment below, the components are created in the
order "one", "two", "three":

class Foo {
public:
 Foo(int a, int b, int c) : two(a), three(b), one(c) { }
 Foo(int a) : one(a), three(b), two(5) { }
 ~Foo() { }

private:
 Gumby one;
 Gumby two;
 Gumby three;
};


ie. order of init is order of declaration, not order of appearance
in constructor initialization.

I thought it was pretty silly, until it was explained that without
the above interpretation, the destructor would not know which order
to destroy the components in order to ensure destruction is done in
the opposite order to construction.

--
Warwick
  _-_|\      warwick@cs.uq.oz.au            /Disclaimer:
 /     * <-- Computer Science Department,  /
 \_.-._/     University of Queensland,    /   C references are NULL && void*
      v      Brisbane, Australia.        /




Author: pjl@cs.uiuc.edu (Paul Lucas)
Date: Wed, 24 Mar 1993 23:18:42 GMT
Raw View
In <12569@uqcspe.cs.uq.oz.au> warwick@cs.uq.oz.au (Warwick Allison) writes:

>pkt@lpi.liant.com (Scott Turner) writes:
>>pjl@cs.uiuc.edu (Paul Lucas) writes:

>>>  Is the order of initializations in declarations defined?

>>It's defined in C by means of sequence points.

>BEWARE.  For the code fragment below, the components are created in the
>order "one", "two", "three":

>class Foo {
>public:
> Foo(int a, int b, int c) : two(a), three(b), one(c) { }
> Foo(int a) : one(a), three(b), two(5) { }
> ~Foo() { }

>private:
> Gumby one;
> Gumby two;
> Gumby three;
>};


>ie. order of init is order of declaration, not order of appearance
>in constructor initialization.

>I thought it was pretty silly, until it was explained that without
>the above interpretation, the destructor would not know which order
>to destroy the components in order to ensure destruction is done in
>the opposite order to construction.

 This isn't what I meant at all; again I meant:

  int i = 0;
  int j = ++i, k = i;

 Is k == 1 guaranteed?  The answer, which came as the result of
 internal e-mail between Jim Coplien, Bjarne, et al, is "yes."
--
 - Paul J. Lucas
   AT&T Bell Laboratories
   Naperville, IL




Author: jimad@microsoft.com (Jim Adcock)
Date: 26 Mar 93 18:13:14 GMT
Raw View
In article <C4F237.3IF@cs.uiuc.edu> pjl@cs.uiuc.edu (Paul Lucas) writes:
|  int i = 0;
|  int j = ++i, k = i;
|
| Is k == 1 guaranteed?

Remember, the C++ standardization effort is being built on *two*
reference documents not one.  If your answer isn't clearly in ARM
then check out the other reference document:

ANSI X3.159-1989 "Programming Language - C"  [also available in ISO]

Looking up "sequence points" in the index give two excellent references

2.1.2.3  -- a discussion of how the 'C' [hence C++] abstract machine works,
and sequence points therein

Appendix B -- a list of all the sequence points.





Author: pjl@cs.uiuc.edu (Paul Lucas)
Date: Wed, 17 Mar 1993 18:32:26 GMT
Raw View
 Is the order of initializations in declarations defined?  Given:

  int i = 0;
  int j = ++i, k = i;

 Is k guaranteed to be 1?  If the second line is equivalent to:

  int j = ++i;
  int k = i;

 then the answer is yes; but is it equivalent to that?  I can't
 find anything in the ARM about it.
--
 - Paul J. Lucas
   AT&T Bell Laboratories
   Naperville, IL




Author: pkt@lpi.liant.com (Scott Turner)
Date: Thu, 18 Mar 1993 16:09:13 GMT
Raw View
In article <C41q63.MxE@cs.uiuc.edu>, pjl@cs.uiuc.edu (Paul Lucas) writes:
>  Is the order of initializations in declarations defined?

>  I can't find anything in the ARM about it.

It's defined in C by means of sequence points.  The C++ standards committee
expects to adopt sequence points or a substitute for them, in order
to retain a number of important properties of C.
--
Prescott K. Turner, Jr.
Liant Software Corp. (developers of LPI languages)
959 Concord St., Framingham, MA 01701 USA    (508) 872-8700
UUCP: uunet!lpi!pkt                          Internet: pkt@lpi.liant.com