Thread overview
what's the use of empty structs in C-style languages ?
Dec 02
Basile B.
Dec 03
Basile B.
December 02

I've noticed that empty struct are always at least a size of 1, see D explorer. 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 ?

December 02
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

December 02
On Monday, December 2, 2024 10:03:22 AM MST Basile B. via Digitalmars-d-learn 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 ?

Sometimes, structs or classes are used to just group functions, and they don't actually have any data. And there are probably other uses that I'm not thinking of at the moment. Really though, I don't know why it's a problem that they're a thing. They behave just like any other struct, and I've never seen any problems caused by them.

Personally, I've used them in unit tests quite a bit when testing stuff that doesn't need the structs to have actual data (e.g. because it was testing a trait that was related to the functions on the struct). Making such structs illegal would cause both druntime and Phobos to fail to compile for exactly that reason. But it's certainly true that I haven't seen many cases where using a struct with no members in an actual program made much sense.

- Jonathan M Davis



December 03

On Monday, 2 December 2024 at 17:03:22 UTC, Basile B. wrote:

>

Big question is what is the problem if we specify that empty struct is an error ?

It's pretty useful in combination with std.sumtype.

December 03
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).