Structs can't be zero bytes in size?

growler growlercab at gmail.com
Mon Sep 2 20:57:08 PDT 2013


On Tuesday, 3 September 2013 at 03:08:34 UTC, Vladimir Panteleev 
wrote:
> On Tuesday, 3 September 2013 at 02:31:44 UTC, Lionello Lunesu 
> wrote:
>> On 9/3/13 8:34, Vladimir Panteleev wrote:
>>> On Tuesday, 3 September 2013 at 00:05:04 UTC, Walter Bright 
>>> wrote:
>>>> On 9/2/2013 4:57 PM, Dylan Knutson wrote:
>>>>> Can someone shed some light on this?
>>>>
>>>> It comes from C. This was done in C so that addresses of 
>>>> struct
>>>> instances will always be unique.
>>>
>>> Why is that important, and why does D need it?
>>>
>>> In C, this might make some sense, however empty structs are 
>>> much more
>>> useful in D, e.g. for metaprogramming.
>>
>> struct Z {};
>> Z a, b;
>> assert(&a != &b);
>
> Yes, but why is this important? If you declare Z as "alias 
> int[0] Z", the assert passes, so why is this property needed 
> for structs but not static arrays?

alias int[0] Y;
Y.sizeof == 0 // Bug?
Y a;
a.length == 0; // Correct.

This is a bug IMO because the size of Y is 1 byte, not 0.

struct Z{}
Z.sizeof = 1;
Z a,b;

If I see code like this I expect a & b to have different 
addresses. But then I'm heavily influenced by C/C++. I'd prefer a 
different syntax, say:

union struct Z{}
or
immutable struct Z{}

either of these reflect that all instances will share the same 
memory location.

G.


More information about the Digitalmars-d mailing list