Forward references and more

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 12 18:26:50 PDT 2009


On Mon, 12 Oct 2009 15:38:07 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Steven Schveighoffer:
>
>> It looks strange what you are doing.  A Foo can have a memory pool of a
>> lot of Foo's?  Do you mean to make the memory pool static?
>
> Right and yes.
>
>
>> I think that might work.<
>
> It works if I use a global variable. But I'd like to not used global  
> variables when possible.

A static variable is essentially a scoped global variable.  I think it  
will work if you make it static.  I've used plenty of static variables  
that are instances of the struct they are declared in.

>
>
>> I think the main problem is you are defining MemoryPool!(Foo).Chunk  
>> which
>> specifically needs to know the size of Foo before Foo is completely
>> declared.
>> It's like you are doing this:
>> struct X
>> {
>>    X x;
>> }
>> Which clearly is incorrect.
>
> But MemoryPool.sizeof is always 8 (on a 32 bit system) because an alias  
> takes no space. So T.sizeof must be 12. I'd like the compiler to  
> understand this.

But you are also declaring the type of the chunk.  I don't think it would  
complain if you were not trying to define a type that required the size of  
Foo.  For example, if you did something like:

struct MemoryPool(T)
{
   T[] chunks;
}

I think it would work, because you aren't trying to define a type which  
*requires* the size of T before T is fully declared.  It's sort of a  
chicken and egg thing.  But since I think you are implementing the memory  
pool incorrectly (it makes no sense for each instance of an item to have a  
pool of itself), you should reexamine what you are trying to do.

-Steve


More information about the Digitalmars-d-learn mailing list