September 03, 2013 Re: Structs can't be zero bytes in size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dylan Knutson | On 9/3/2013 1:01 PM, Dylan Knutson wrote: > 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. If you need a zero sized object in D, alias whatever[0] zeroSizeObject; > 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? Yes. |
September 03, 2013 Re: Structs can't be zero bytes in size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Tuesday, 3 September 2013 at 20:07:57 UTC, Walter Bright wrote:
> If you need a zero sized object in D,
>
> alias whatever[0] zeroSizeObject;
I tried that, but unfortunately std.variant isn't compatible with zero sized types:
```
import std.variant;
import std.stdio;
import std.typecons;
void main() {
// This would be preferable, but Algebraic doens't like zero sized types
//alias FirstType = void[0];
//alias SecondType = void[0];
// phobos\std\variant.d(165): Error: static assert (0u >= 4u) is false
// phobos\std\variant.d(1149): instantiated from here: VariantN!(0u, void[0u], void[0u])
// Typedef!() wraps void[0] in a struct, which still has a size, and therefore
// memory overhead, so we're back to square one.
alias FirstType = Typedef!(void[0]);
alias SecondType = Typedef!(void[0]);
pragma(msg, FirstType.sizeof); // 1UL, darn
alias Variants = Algebraic!(FirstType, SecondType);
}
```
|
September 03, 2013 Re: Structs can't be zero bytes in size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dylan Knutson | On 9/3/13, Dylan Knutson <tcdknutson@gmail.com> wrote: > I tried that, but unfortunately std.variant isn't compatible with zero sized types: Please do file this as a bug: http://d.puremagic.com/issues/enter_bug.cgi?product=D If you don't have the time, we'll file it for you. Thanks! |
September 03, 2013 Re: Structs can't be zero bytes in size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Tuesday, 3 September 2013 at 20:42:47 UTC, Andrej Mitrovic wrote: > On 9/3/13, Dylan Knutson <tcdknutson@gmail.com> wrote: >> I tried that, but unfortunately std.variant isn't compatible with >> zero sized types: > > Please do file this as a bug: > http://d.puremagic.com/issues/enter_bug.cgi?product=D > > If you don't have the time, we'll file it for you. Thanks! Reported. I didn't realize this wasn't the intended behavior of Variant. :-) http://d.puremagic.com/issues/show_bug.cgi?id=10958 |
September 03, 2013 Re: Structs can't be zero bytes in size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 09/03/2013 12:35 PM, Walter Bright wrote: > Some background: Ooh! This one has a very interesting excerpt: http://stackoverflow.com/a/10971748 "If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined." So the question is moot because empty struct has undefined behavior... in C... perhaps in D as well... Sweet! :) Ali |
September 03, 2013 Re: Structs can't be zero bytes in size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 9/3/2013 2:43 PM, Ali Çehreli wrote:
> On 09/03/2013 12:35 PM, Walter Bright wrote:
>
>> Some background:
>
> Ooh! This one has a very interesting excerpt:
>
> http://stackoverflow.com/a/10971748
>
> "If the struct-declaration-list does not contain any named members, either
> directly or via an anonymous structure or anonymous union, the behavior is
> undefined."
>
> So the question is moot because empty struct has undefined behavior... in C...
> perhaps in D as well...
>
> Sweet! :)
In C++ it is defined.
|
September 04, 2013 Re: Structs can't be zero bytes in size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 9/3/13 10:59, Andrej Mitrovic wrote:
> On 9/3/13, Lionello Lunesu <lionello@lunesu.remove.com> wrote:
>> struct Z {};
>> Z a, b;
>> assert(&a != &b);
>
> Since the structs are declared empty, their 1-byte values don't
> matter. So their addresses don't really matter either.
>
It has to do with the ol' 'identity vs equality'. In the example above, a and b are not identical ("do not refer to the same thing") and so their addresses should not be equal.
The address of two 'named things' is the same if-and-only-if the names refer to the same thing.
L.
|
Copyright © 1999-2021 by the D Language Foundation