Forward references and more

Steven Schveighoffer schveiguy at yahoo.com
Tue Oct 13 08:33:12 PDT 2009


On Tue, 13 Oct 2009 07:30:11 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Steven Schveighoffer:
>
>> 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.
>
> It doesn't compile with static variables too, you just need few seconds  
> to try it on Codepad:
> http://codepad.org/QHm2QNQ7
>
> struct MemoryPool(T) {
>     alias T[100_000 / T.sizeof] Chunk;
>     static Chunk*[] chunks;
> }
> struct Foo {
>     int x;
>     static MemoryPool!(Foo) pool;
> }
> void main() {}

If that fails, then it seems like a bug to me.

This works:

struct Foo {
    static Foo var;
    int x;
}

which seems to suggest that static variables are treated like globals.   
You should submit a bug to get that fixed.

>> 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.
>
> Next time I show code I'll replace all variable and type names with foo,  
> baz, spam, etc. The memory pool in my dlibs is implemented correctly (I  
> think). The code I've shown is just a reduced case, where I have removed  
> the "static" too :-)

It makes no sense to have a memory pool *per* instance, it makes more  
sense to have one pool for many instances (whether it's static or not).

-Steve


More information about the Digitalmars-d-learn mailing list