Structs can't be zero bytes in size?

Dylan Knutson tcdknutson at gmail.com
Tue Sep 3 13:01:01 PDT 2013


On Tuesday, 3 September 2013 at 19:30:53 UTC, Walter Bright wrote:
> On 9/3/2013 9:00 AM, Piotr Szturmaj wrote:
>> How about specifying extern(C) before each C structure? The 
>> majority of bindings
>> already have "extern(C):" on top of module.
>
> While we could do that, at what point does this become arcane 
> complexity?

I think it depends how much metaprogramming could benefit in the 
language. Algebraic datatypes use zero sized structs in may 
places (see Rust, with Option(T) variants None and Some(T)), and 
I've heard of devs using structs as namespaces. In C, there was 
pretty much no metaprogramming to speak of, so semantic struct 
sizes just weren't a bit deal (as far as I can tell, that is).

But, in D, metaprogramming is a ubiquitous feature, so I do think 
it's reasonable to consider some way of specifying zero sized 
structs in the language. I'd argue for it to be an opt-in feature 
though, so empty structs, by default, can't have a zero size. 
This keeps code backwards compatible.

Perhaps something like `enum struct Foo;`, to indicate that it's 
just a compile-time used distinct type, and not a value? Just 
throwing that out there. I'm sure there is a better way to 
indicate that a type is just used as a type/namespace, without a 
value persay.

When you say retain C compatibility, do you mean having C code 
which makes use of empty structs still be compile-able with the D 
compiler? Or D code inter-oping with external C libraries?


More information about the Digitalmars-d mailing list