|
From: | Edward Rosten |
Subject: | Re: [libcvd-members] gvars3 Makefile.in configure configure.ac gvars... |
Date: | Wed, 20 Feb 2008 16:10:20 -0700 (MST) |
On Wed, 20 Feb 2008, Ethan Eade wrote:
template <int DUMMY> double HoldsGlobal::global = 3.14; //end code Then you use HoldsGlobal<>::global. The linker is required to treat it as a weak symbol -- i.e., there will be exactly one instance common to all translation units when linked.The other problem is the order of calling global constructors. The standard is rather unhelpful about this.Yes, trickier.
One trick is the very closely relates trick to the one above with the lazy instance method:
template<int Dummy=0> class foo { foo(){} static foo* inst=0; public: foo& instance() { if(!foo) inst = new foo(); return *foo; } };The first thing to use it causes it to be initialized. The trouble is the rather irritating syntactic overhead of having to do foo::instance(). instead of foo. everywhere.
The trouble with GVars is that introducing new syntax would break too much old code. I suppose I could gut GUI, make it a trivial class with no state and make all the methods be thin wrappers to a_new_GUI_implementation::instance().blah()
Ick.It does seem to me that there is little point in having GUI be a non singleton, since the GVars database is a singleton.
-Ed
[Prev in Thread] | Current Thread | [Next in Thread] |