🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++.moderated
Article #42

Static STL containers ? Possible ?

#42
From: Bowden Wise
Date: Mon, 22 Jan 1996 09:52
78 lines
2757 bytes
I am creating a singleton class registry.  To implement this, 
I have created a base singleton class, called Singleton.  All of
the other singleton classes are derived from this one, and must
"Register" themselves by calling the base class method:

  Singleton::Register (const char* name, Singleton* instance);

The registry is an STL map:

  static map<string, Singleton*, less<string> > _registry;

which is a static member of the base class Singleton.

Clearly there is only one instance of each derived singleton.  Say
classes A and B are two such derived classes of Singleton.  Then I
have tried creating their instances by placing this:

static A instanceA;
static B instanceB;

before main:

void main ()
{
   // Lookup singleton A:
   Singleton* a = Singleton::Lookup ( "A" );

   if ( a )
   {
   }

}

The static instances of A and B are created BEFORE main() exectues
as expected, however, it aborts during the construction of A.

I get an abort, inside one of the STL tree routines.  

Now, if I move the statics *inside* main:

void main ()
{
   // statics now in main()
   static A instanceA; 
   static B instanceB;

   // Lookup singleton A:
   Singleton* a = Singleton::Lookup ( "A" );

   if ( a )
   {
   }

}

then the aborts do not occur.

Can someone explain why this is the case?  

I am thinking that since C++ does not gurantee the order of the 
construction of statics, 
and  since the STL relies heavily on static variables, that my statics
must be getting constructed before all of the STL static vars have 
been initialized?

Is that possible?  What is the solution?  Do I have to create
the instances via another class that perhaps 'new''s the single
instances ?

--------------------------------------------------------------------
G. Bowden Wise
Computer Science Dept, Rensselaer Polytechnic Inst, Troy, NY 12180
Email: wiseb@cs.rpi.edu         WWW: http://www.cs.rpi.edu/~wiseb/

      [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
      [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
      [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
      [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
Technical Details
Message-ID: <4dvmpc$2eb@netlab.cs.rpi.edu>
Path: arc1-ca.pugleaf.net!library.erc.clarkson.edu!rpi!not-for-mail

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!library.erc.clarkson.edu!rpi!not-for-mail
From: Bowden Wise <wiseb@cs.rpi.edu>
Newsgroups: comp.lang.c++.moderated,comp.lang.c++
Subject: Static STL containers ?  Possible ?
Date: 22 Jan 1996 09:52:44 -0000
Organization: Rensselaer Polytechnic Institute, Computer Science
Sender: cppmods@netlab.cs.rpi.edu
Approved: devitto@ferndown.ate.slb.com
Message-ID: <4dvmpc$2eb@netlab.cs.rpi.edu>
NNTP-Posting-Host: netlab.cs.rpi.edu
X-Original-Date: Mon, 22 Jan 1996 01:13:28 -0800