wrote:
> I have a class that maintains a static std::list of its instances.
> Thus, due to the order of constructors ambiguity of itself and
> std::list, it cannot be safely instantiated until after main is
> called. What I would like to do is put in an assert if it is
> instantiated too early, but unsure how to detect this case. Or perhaps
> there is a better design for instance tracking?
>
Instead of declaring the list as a static member of the class, declare
it as a local static variable in a static function, like so:
class A
{
static std::list<A *> & get_list() ;
A()
{
get_list().push_back(this) ;
}
} ;
std::list<A *> & A::get_list()
{
static std::list<A *> instances ;
return instances ;
}
You are guaranteed that 'instances' will be constructed the first time
control passes over it, which should eliminate your problem.
--
Alan Johnson