Structs can't be zero bytes in size?

Dylan Knutson tcdknutson at gmail.com
Tue Sep 3 13:24:05 PDT 2013


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);
}
```


More information about the Digitalmars-d mailing list