Non-recursive maxSizeOf

H. S. Teoh hsteoh at
Thu Aug 6 13:28:31 UTC 2020

On Thu, Aug 06, 2020 at 01:07:14PM +0000, Per Nordlöw via Digitalmars-d-learn wrote:
> However, your solution
> template maxSize(T...)
> {
>     align(1) union Impl { T t; }
>  	enum maxSize = Impl.sizeof;
> }
> fails as
> variable `std.variant.maxSize!(void, string).Impl.__t_field_0`
> variables cannot be of type `void`
> because one of the instances of `maxSize` in std.variant has `void` as
> a member `T...`.

Ugh, apparently `void` is one of those nasty inconsistent things that
has all sorts of ad hoc behaviour:

- void cannot be used to instantiate variables (behaves sortof like a
  bottom type);
- But in spite of that, void.sizeof == 1, rather than the expected 0. My
  guess is that this has to do with the hack to make the size of void[]
  equal to its length;
- void in void[] is a top type (but only in that context -- but its
  .sizeof is inconsistent with this);
- void is a unit type when specified as a function return type (but in
  spite of that cannot be instantiated, contrary to a true unit type,
  and this contradicts its being a top type in void[]);

IOW, void is a mishmash of ad hoc things thrown together in a way that
isn't even self-consistent. A chimera of exceptions that, it would seem,
always require special-casing to handle.

> Do you have any simple solution to this, H. S. Teoh?

Unfortunately, no. :-(  Why does std.variant even support storing void
in the first place?!


People tell me that I'm paranoid, but they're just out to get me.

More information about the Digitalmars-d-learn mailing list