 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Stephan Keil Guest
|
Posted: Thu Nov 18, 2004 12:16 pm Post subject: Destruction order (esp. singletons) |
|
|
Hi all,
consider a class
class X {
static X& OneX() { static X sgl1; return sgl1; }
static X& TheX() { static X sgl2(OneX()); return sgl2; }
};
and two source files with global variables
// a.cpp
X a1;
X a2(X::TheX());
// b.cpp
X b1;
X b2(X::TheX());
As I understand it, two valid construction orders are
"a1 sgl2 sgl1 a2 b1 b2" and "b1 sgl2 sgl1 b2 a1 a2".
Now, two questions:
1. Is "a1 b1 sgl2 sgl1 b2 a2" also a valid construction order
(i.e. is it guaranteed that the global objects of a
translation unit are all constructed before any global object
of another translation unit)?
2. I would find it natural if the destruction order follows
always the reverse construction order.
But what does the standard say about the destruction order
(esp. for sgl1 and sgl2)? (And do current compiler follow
the standard in that point;-?)
Thanks, Stephan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Nov 19, 2004 4:04 pm Post subject: Re: Destruction order (esp. singletons) |
|
|
Stephan Keil wrote:
| Quote: | consider a class
class X {
|
public:
| Quote: | static X& OneX() { static X sgl1; return sgl1; }
static X& TheX() { static X sgl2(OneX()); return sgl2; }
};
and two source files with global variables
// a.cpp
X a1;
X a2(X::TheX());
// b.cpp
X b1;
X b2(X::TheX());
As I understand it, two valid construction orders are
"a1 sgl2 sgl1 a2 b1 b2" and "b1 sgl2 sgl1 b2 a1 a2".
|
Nope. 'sgl1' should be constructed before 'stl2' because it is used to
create the temporary with which 'sgl2' is _later_ direcly initialised.
The storage for 'sgl2' may be allocated before 'sgl1', but it has no
effect on the construction order. The order is determined by the _end_
of each respective constructor. So, the possible construction orders
IMO are many. Here is just the beginning of all possible combinations:
a1 (sgl1 sgl2 a2) b1 b2
b1 (sgl1 sgl2 b2) a1 a2
a1 b1 (sgl1 sgl2 b2) a2
a1 b1 (sgl1 sgl2 a2) b2
| Quote: |
Now, two questions:
1. Is "a1 b1 sgl2 sgl1 b2 a2" also a valid construction order
(i.e. is it guaranteed that the global objects of a
translation unit are all constructed before any global object
of another translation unit)?
|
No. The only thing that is guaranteed is that the objects declared in
the same translation unit are constructed in the order of declaration.
| Quote: | 2. I would find it natural if the destruction order follows
always the reverse construction order.
|
It is so.
| Quote: | But what does the standard say about the destruction order
(esp. for sgl1 and sgl2)? (And do current compiler follow
the standard in that point;-?)
|
It does say that the destruction is the reverse, and all compilers I used
do follow that requirement.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephan Keil Guest
|
Posted: Sat Nov 20, 2004 10:19 am Post subject: Re: Destruction order (esp. singletons) |
|
|
| Quote: | Nope. 'sgl1' should be constructed before 'stl2' because it is used to
create the temporary with which 'sgl2' is _later_ direcly initialised.
|
Yes, of course, my mistake.
| Quote: | It does say that the destruction is the reverse, and all compilers I used
do follow that requirement.
|
That helps a lot:) Thanks for the answer,
Stephan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Nov 22, 2004 11:09 am Post subject: Re: Destruction order (esp. singletons) |
|
|
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote
[...]
| Quote: | But what does the standard say about the destruction order
(esp. for sgl1 and sgl2)? (And do current compiler follow the
standard in that point;-?)
It does say that the destruction is the reverse, and all compilers I
used do follow that requirement.
|
A lot of older compilers got this wrong when local statics were
concerned. (At least one older compiler even destructed local statics
that were never constructed.)
Still, it's been a long time since I saw this error.
--
James Kanze GABI Software http://www.gabi-soft.fr
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! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|