Why is my structure template does not compile?

Denis Koroskin 2korden at gmail.com
Thu Dec 11 16:56:27 PST 2008


On Fri, 12 Dec 2008 02:47:42 +0300, BCS <ao at pathlink.com> wrote:

> Reply to Denis,
>
>> On Fri, 12 Dec 2008 02:21:11 +0300, BCS <ao at pathlink.com> wrote:
>>
>>> Reply to Weed,
>>>
>>>> invariant()
>>>> {
>>>> // If I comment out next line compilation goes smoothly:
>>>> assert( Element.sizeof > 0 );
>>>> }
>>> OTOH that assert is wrong. Element.sizeof will always return 8, the
>>> size  of an array reference. What you want is Element.length.
>>>
>> No, I don't think so. Don't confuse 'Element' with 'data'. Element is
>> a type, it doesn't have a length property. But it does have sizeof (an
>> example of Element is an 'int', which is 4 bytes long).
>>
>
> Oh, fud. I did get that wrong. I still think it's wrong as I'm not sure  
> anything has 0 sizeof and even then that should be static assert.
>
>

No, zero-sized arrays are allowed in D:

struct Palette
{
   uint    size;
   ARGB[0] colors
}

Palette* createPalette(int numColors)
{
    return cast(Palette*)GC.malloc(uint.sizeof + numColors * ARGB.sizeof);
}


More information about the Digitalmars-d-learn mailing list