Non-recursive maxSizeOf

Per Nordlöw per.nordlow at gmail.com
Thu Aug 6 13:07:14 UTC 2020


On Thursday, 6 August 2020 at 12:51:22 UTC, H. S. Teoh wrote:
> Of course. In fact, it's trivial:
>
> ------
> template maxSizeOf(T...)
> {
> 	align(1) union Impl {
> 		T t;
> 	}
> 	enum maxSizeOf = Impl.sizeof;
> }

I originally copied my original version of `maxSizeOf` from 
`std.variant.maxSize` currently being

template maxSize(T...)
{
     static if (T.length == 1)
     {
         enum size_t maxSize = T[0].sizeof;
     }
     else
     {
         import std.algorithm.comparison : max;
         enum size_t maxSize = max(T[0].sizeof, maxSize!(T[1 .. 
$]));
     }
}

.

It should be updated to use this trick.

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...`.

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


More information about the Digitalmars-d-learn mailing list