Article View: comp.std.c++
Article #3842Re: static members as members of a metaclass h
From: Steve Clamage
Date: Thu, 28 Mar 1996 09:26
Date: Thu, 28 Mar 1996 09:26
53 lines
3030 bytes
3030 bytes
In article doh@arl-news-svc-2.compuserve.com, Philippe Verdy <100105.3120@compu
serve.com> writes:
>Many problems in C++ are related to the need of using some
>static members in the definition of the class.
>
>There are many cases where those static members have to be
>initialized in a proper sane order, as for any instances of
>the class they belong.
>
>Shamely, the current C++ definition does not allow for a clean
>specification of which (and when) classes are constructed
>prior to using them for the construction of other classes.
The lack of specification of initialization order across modules is
not due to lack of trying. Many proposals have been made, and none
have been considered adequate. Your proposal for metaclasses is too big
a change to be considered in this round of standardization.
I believe a reasonable solution to the problem of initialization of non-local
static data is not to use non-local static data!
Instead of a static data object or static class data member, use a
function with a local static object. Example:
T1& global_T1() { static t1(args); return t1; } // instead of T1 global_T1;
class C {
static T2& static_T2(); // instead of T2 static_T2;
};
T2& C::static_T2() { static T2 t2; return t2; }
The only way to access static objects t1 or t2 is to call a function.
The first time the access function is called, the object is initialized
then, and only then. You have whatever control over the order of
initialization you need. Typically the order will be implicitly correct,
since no static data can be used until it is initialized.
Only two features are missing, compared to non-local static objects:
1. If you never reference an object, it is never initialized.
2. The order of destruction remains unspecified. If that is a problem,
possibly the static objects could be of a type which has a do-nothing
destructor and a "destroy" member function which could be called
explicitly at the proper time.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<4jckdc$ad6@engnews1.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<4jc5lt$doh@arl-news-svc-2.compuserve.com>Show Full Headers
Complete Article Headers
From: clamage@Eng.Sun.COM (Steve Clamage) Message-ID: <4jckdc$ad6@engnews1.Eng.Sun.COM> X-Original-Date: 27 Mar 1996 23:53:16 GMT Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 28 Mar 96 09:26:03 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: static members as members of a metaclass h Organization: Sun Microsystems Inc. References: <4jc5lt$doh@arl-news-svc-2.compuserve.com> Reply-To: clamage@Eng.Sun.COM X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMVpbOOEDnX0m9pzZAQELFAF+Php+hlbBb9P/mgA2s5toT8V4cBAOUD9b fRsfZK2oeA0hpiCrFuHfEVFMVdw9l8Pr =zS80