Forward references and more

Ary Borenszweig ary at
Mon Oct 12 12:59:37 PDT 2009

Steven Schveighoffer wrote:
> On Mon, 12 Oct 2009 05:26:58 -0400, bearophile 
> <bearophileHUGS at> wrote:
>> What's wrong with this code?
>> struct MemoryPool(T) {
>>     alias T[100_000 / T.sizeof] Chunk;
>>     Chunk*[] chunks;
>> }
>> struct Foo {
>>     int x;
>>     MemoryPool!(Foo) pool;
>> }
>> void main() {}
>> It prints "Error: struct problem.Foo no size yet for forward reference".
>> T.sizeof must be 8 in all cases.

The compiler does this:

Instantiate MemoryPool!(Foo)
But for that it needs to know Foo.sizeof.
But for that it needs to know MemoryPool!(Foo).sizeof.
But for that it needs to instantiate MemoryPool!(Foo) and find it's size.

I can see MemoryPool!(Foo) sizeof doesn't depend at all of T.sizeof 
because it just has a pointer. But how do you suggest to fix the 
compiler to understand that?

What I can see is that to know MemoryPool!(Foo).sizeof the type of the 
alias doesn't need to be solved completely... but what would you 
recommend the compiler to do?

More information about the Digitalmars-d-learn mailing list