what's the use of empty structs in C-style languages ?

Ali Çehreli acehreli at yahoo.com
Mon Dec 2 19:15:07 UTC 2024


On 12/2/24 9:03 AM, Basile B. wrote:
> I've noticed that empty struct are always at least a size of 1, see [D 
> explorer](https://godbolt.org/z/sGYcex4cf). There you can see 
> `%example.S = type { [1 x i8] }`
> 
> So te compiler inserts a dummy member so that that everything can work 
> as usual.
> 
> Big question is what is the problem if we specify that empty struct is 
> an error ?

Then you couldn't have arrays of them where elements having different 
addresses. Because of size being 1, today this passes:

struct S {}

void main() {
     S[2] ses;
     assert(&ses[0] != &ses[1]);
}

Ali



More information about the Digitalmars-d-learn mailing list