Opaque structs
monarch_dodra
monarchdodra at gmail.com
Sun Jun 30 03:48:19 PDT 2013
On Sunday, 30 June 2013 at 08:18:39 UTC, Johannes Pfau wrote:
> Am Sat, 29 Jun 2013 17:38:38 +0200
> schrieb "monarch_dodra" <monarchdodra at gmail.com>:
>
>> On Saturday, 29 June 2013 at 08:01:17 UTC, Johannes Pfau wrote:
>> > Shouldn't doing anything value-related on
>> > an empty struct be invalid anyway?
>>
>> Why ?
>>
>> The fact that the struct has no members is an implementation
>> detail which should have no impact on the user of the struct.
>
> It's probably a matter of perception. As you said in your other
> post
> there are good reasons to give empty structs a size. But if you
> (naively) think of a struct as a simple aggregate of other
> types, then a
> aggregate of zero other types should have size zero.
Well, *technically*, it should have a size of *at least* 0, since
the compiler is allowed to add as much padding as it wishes
(which it does).
> There's no
> information in such a struct which would have to take up space.
> And
> doing something value-related on some type which doesn't have a
> size
> and therefore doesn't have a value is not really well-defined.
> (How do
> you copy a value of size 0? What happens if you dereference a
> pointer
> to a value of size 0?).
Well, as you say, it is a matter of perception: From the client
side, all the client should know is that the structs hold no
"information", the actual *size*, is not his problem: EG, it is
an empty "bag". The fact that the bag is empty though shouldn't
prevent the client from having an array of bags, or to have
pointers to the bag.
If the client can't say "I created an S, which is a struct that
holds no *data*, and this is it's address", then there is a
problem, and it is the implementation's fault.
The compiler works around that problem by giving the empty struct
a size. It is a "dirty" way to do it, but it works :)
More information about the Digitalmars-d-learn
mailing list