|
From: | Georg Klein |
Subject: | Re: [libcvd-members] gvars3 Makefile.in configure configure.ac gvars... |
Date: | Wed, 20 Feb 2008 23:05:48 -0000 |
Ethan Eade wrote:
You can always use the template trick, which requires the linker to ensure that only one instance is created: // code in a header: template <int DUMMY=0> struct HoldsGlobal { double global;Should read "static double global;"}; 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.
Unrelated to GVars, but while we're on the topic of statics and singletons: One mechanism for a singleton class foo is to have
foo& foo::getinstance() { static foo f; return f; }but the page where I read about this said something along the lines of ``use only when single-threaded.'' Why might that be?
[Prev in Thread] | Current Thread | [Next in Thread] |