Topic: How to force static initialization?


Author: bobkf@news.delphi.com (BOBKF@DELPHI.COM)
Date: 2 Jan 1994 17:43:37 -0500
Raw View
According to ARM, initialization of nonlocal static objects can be
"deferred to any point in time before the first use of a function
or object defined in that translation unit." A problem here is
that classes which are initialized by static constructors and
are only constructed through static member functions of the
same class (in the same compilation unit) are never initialized
at all. For example,

class bar {
  bar(foo *(*f)()) { newFun = f; }
  foo *MakeNewFoo() = { return *newFun; }
  static foo *(*newFun)();
};
class foo {
  foo();
  static bar gBar;
};

static foo *MakeFooFun() { return new foo(); }
bar foo::gBar(MakeFooFun);
foo:foo() {...}

A runtime exercising its option to defer static initialization
to "first use" seems never to initialize gBar at all.

Is this runtime correctly interpreting the standard? If so,
what is the simplest way to ensure gBar is initialized?

Bob Foster
Object Factory
objfactory@aol.com