Topic: static members as members of a metaclass hierachy
Author: Philippe Verdy <100105.3120@compuserve.com>
Date: 1996/03/27 Raw View
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.
Why does the C++ standard evolve to a more solid definition
including metaclasses to solve this problem:
So we could instantiate the class in the metaclass constructor.
One can think of creating a collection of classes as being
instances of a metaclass.
How implement it ?
Simply we would define a metaclass like any other C++ class,
but each class constructor (the code which instantiate static
members of the class, as opposed to an instance constructor
which instantiate non static members) could be more safely
called and specified in the class definition itself.
So we would have to manage with a structure like this :
class X : public Y, static M
{
//////////// class construction part ////////////////////
static X()
: M(parameters) // metaclass construction
, static_member1(parameters) // static members construction
{
// code to initilialize static members of X
// after calling static class M constructor
}
static virtual ~X()
{
// code to delete static members of X
// then the static class M destructor will be called
}
static class_member1;
///////////////// instance definition part //////////////
X() // classic instance constructor
: Y (parameter) // base class instance construction
, instance_member2(parameter)
{...}
~X() {...} // classic instance destructor
int instance_member2;
};
Why using static (metaclass) inheritance ?
Because it will ensure that this metaclass is constructed
before initializing class static members.
So it can provide the monitoring which allows classes to
be safely constructed and deleted.
The class would not be really instances of the metaclass
because each metaclass-instance (or class) is fully specified
and cannot be instantiated without defining its behavior.
Then what about static members initializers ?
this would only remain as a common way of initializing them
when there is no static class constructor defined. In that
case, the compiler generates itself the class constructor and
the class destructor, by aggregating all static members
initializer in a compiler-generated function.
When would the metaclass be constructed ? Like any other
classes which are instanciable : by defining a static instance
of the metaclass like this :
static class M; // valid if static M::M() exists
static class X; // valid if static X::X() exists
We do not need then to add the whole list of static members
instances because the class constructor code instantiate
them itself, in the specified order.
How does the compiler control class instantiation order ?
by reading the list of their static inheritances, and the
compiler could manage a flag within the virtual table of
each class, which is specifically devoted to this task.
There can also be some virtual class destructors I think, but
I'm not sure if this would be useful or not.
What do you think of this proposal ? Would it provide
additional problems ? Are there proposals which similarily
go in this sense ?
---
[ 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 ]