Topic: Static member initialization in template classes.
Author: esap@cs.tut.fi (Pulkkinen Esa,,0594,OHJ,150651,1137)
Date: Thu, 24 Feb 1994 09:54:58 GMT Raw View
Recently I made the following code, which I couldn't get compiled:
template <class T>
class FreeMemoryList
{
T *item;
FreeMemoryList<T> *next;
static FreeMemoryListPointer<T> free_memory;
.
.
.
public:
static T *alloc();
static void free(T *);
};
Where the FreeMemoryListPointer is a class containing a
pointer to the FreeMemoryList, and handling the correct cleanup
of the list.
The intention is to make a "global" list to store all instances
of some class and control their memory allocation by providing
a couple of static functions (FreeMemoryList<T>::alloc and
FreeMemoryList<T>::free), which would allocate enough room for
an object of type T. For each type, a list of free memory is
maintained. The basic idea is to minimize the storage allocation
overhead when using lot's of news and deletes by storing a list
of freed memory, and if the list is empty, then use the system's
memory-allocation functions, and otherwise just return one of
the previously freed memory. Templates are used to ensure that
the size of the blocks being allocated remains constant, so that
no memory fraqmentation occurs inside the system.
However, I can't figure out how to initialize the free_memory-variable (and you have to do it to get
the program compiled). I have tried a couple of different
variants like:
template <class T>
FreeMemoryListPointer<T> FreeMemoryList<T>::free_memory;
This doesn't work since there's no template data members.
I *can* initialize it for each individual class created by
the template, but can't seem to get a template do it for me.
For example:
FreeMemoryListPointer<int> FreeMemoryList<int>::free_memory;
This initializes it for the int-type, but this approach isn't
suitable for a library, because one would expect that the
library does initialize all the *private* data members it uses.
And the user of the library shouldn't even have to know, that
there exists a class containing the free_memory-variable.
Comments? Suggestions?
---
------------ Esa Pulkkinen, esap@cs.tut.fi ------------------------
What you see is not what you get, and why would it be, because
that`s the least thing you would have wanted.
-------------------------------------------------------------------
Author: olaf@cwi.nl (Olaf Weber)
Date: Thu, 24 Feb 1994 13:27:43 GMT Raw View
In article <1994Feb24.095458.10217@news.cs.tut.fi>, esap@cs.tut.fi (Pulkkinen Esa,,0594,OHJ,150651,1137) writes:
[shortened, and stuff moved around]
> Recently I made the following code, which I couldn't get compiled:
> template <class T>
> class FreeMemoryList
> {
> T *item;
> FreeMemoryList<T> *next;
> static FreeMemoryListPointer<T> free_memory;
> .
> .
> .
> public:
> static T *alloc();
> static void free(T *);
> };
> template <class T>
> FreeMemoryListPointer<T> FreeMemoryList<T>::free_memory;
> Where the FreeMemoryListPointer is a class containing a
> pointer to the FreeMemoryList, and handling the correct cleanup
> of the list.
This is perfectly legal, (ARM r.14.8), but many compilers haven't
implmented it yet. Use the explicit version
> FreeMemoryListPointer<int> FreeMemoryList<int>::free_memory;
as a workaround, or find a better compiler.
Followups to comp.lang.c++
-- Olaf Weber