Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Destruction order (e.g. singletons)

Thread Tools

Destruction order (e.g. singletons)

Stephan Keil
Posts: n/a
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
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Order of destruction of static members and static objects Victor Bazarov C++ 11 12-09-2009 08:54 AM
Order of destruction Lilith C++ 3 04-04-2008 07:40 AM
virtual base class and construction/destruction order BeautifulMind C++ 7 02-08-2007 12:35 PM
initialization and destruction order for class members Dennis Jones C++ 2 01-05-2007 12:53 AM
Order of destruction of static locals w.r.t. globals Davlet Panech C++ 4 11-10-2006 08:51 PM