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

Basile B. b2.temp at gmx.com
Tue Dec 3 12:53:54 UTC 2024


On Monday, 2 December 2024 at 19:15:07 UTC, Ali Çehreli wrote:
> 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

The question was also "is it a problem to make empty structs an 
error".

Anyway thanks to all for your answers but it turns out that the 
reason why I opened the topic was not right. It was related to a 
bug in styx, that happens also with non-empty structs. 
(https://gitlab.com/styx-lang/styx/-/issues/746).


More information about the Digitalmars-d-learn mailing list