Topic: A puzzle involving object destruction (try
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Wed, 2 Nov 1994 03:00:16 GMT Raw View
je@unix.brighton.ac.uk (John English) writes:
>Fergus Henderson (fjh@mundil.cs.mu.OZ.AU) wrote:
>: Yes, but only for non-local statics. Local static variables
>: are initialized when their containing scope is entered.
>
>Surely you mean "when their declarations are elaborated for the first time"?
Yes. (The point was that initialization of non-local statics depends
on the program's dynamic behaviour, not just the order of the declarations.)
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: jason@cygnus.com (Jason Merrill)
Date: Mon, 24 Oct 1994 22:06:01 GMT Raw View
>>>>> David Binderman <dcb@lovat.fmrco.com> writes:
> In article EF2@netcom.com, rfg@netcom.com (Ronald F. Guilmette) writes:
>> I tried three different compilers (all on UNIX) and got three different
>> set of results, i.e.:
>> Compiler 1 Compiler 2 Compiler 3
>> --------------------------------------------
>> ~T2 ~B ~B
>> ~T1 ~A ~A
>> ~B ~T1 ~T2
>> ~A ~T2 ~T1
>> Which compiler is right?
> 3. Order of decl in the file matters.
Actually, all three are conformant ("right" may be a separate matter). For
A and B, order does matter. For T1 and T2, it does not. From the
September WP:
6.7 Declaration statement [stmt.dcl]
5 The destructor for a local object with static storage duration will be
executed if and only if the variable was constructed. The destructor
must be called either immediately before or as part of the calls of
the atexit() functions (_basic.start.term_). Exactly when is unspeci-
fied.
I would argue that the "right" output would be
~T2
~B
~T1
~A
as that would reproduce the actual reverse of the order of construction.
This would require a lot more machinery than any of the three solutions
above, however.
Jason
Author: fjh@mundil.cs.mu.OZ.AU (Fergus Henderson)
Date: Wed, 26 Oct 1994 18:54:36 GMT Raw View
dcb@lovat.fmrco.com (David Binderman) writes:
>rfg@netcom.com (Ronald F. Guilmette) writes:
>
>>Which compiler is right?
>
>3. Order of decl in the file matters.
Yes, but only for non-local statics. Local static variables
are initialized when their containing scope is entered.
Destruction is in the reverse order to initialization.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: je@unix.brighton.ac.uk (John English)
Date: Fri, 28 Oct 1994 13:15:26 GMT Raw View
Fergus Henderson (fjh@mundil.cs.mu.OZ.AU) wrote:
: Yes, but only for non-local statics. Local static variables
: are initialized when their containing scope is entered.
Surely you mean "when their declarations are elaborated for the first time"?
--
-------------------------------------------------------------------------------
John English | Thoughts for the day:
Dept. of Computing | - People who live in windowed environments
University of Brighton | shouldn't cast pointers
E-mail: je@brighton.ac.uk | - In C++ only your friends can access your
Fax: 0273 642405 | private parts
-------------------------------------------------------------------------------
Author: dcb@lovat.fmrco.com (David Binderman)
Date: Mon, 24 Oct 1994 12:04:16 GMT Raw View
In article EF2@netcom.com, rfg@netcom.com (Ronald F. Guilmette) writes:
>I tried three different compilers (all on UNIX) and got three different
>set of results, i.e.:
> Compiler 1 Compiler 2 Compiler 3
> --------------------------------------------
> ~T2 ~B ~B
> ~T1 ~A ~A
> ~B ~T1 ~T2
> ~A ~T2 ~T1
>Which compiler is right?
3. Order of decl in the file matters.
I tried your code on a respected commercial C++ compiler and got the same
as compiler 3. I also tried your code on a derivative of cfront 3, and it wouldn't
compile, (giving sorry not implemented msgs) until I'd hacked it about a bit.
cfront 3 eventually produced the same as compiler 3.
>What do other compilers do?
Provide lots of variation in what they do.
Regards
David C Binderman MSc BSc +44 171 975 4723 dcb@lovat.fmrco.com
UNIX, C, C++ - almost certainly more than you need to know.