Non-recursive maxSizeOf

Ali Çehreli acehreli at yahoo.com
Thu Aug 6 01:13:28 UTC 2020


On 8/5/20 5:58 PM, Per Nordlöw wrote:
> Is it possible to implement
> 
> template maxSizeOf(T...)
> {
>      static if (T.length == 1)
>          enum size_t maxSizeOf = T[0].sizeof;
>      else
>      {
>          enum size_t firstSize = T[0].sizeof;
>          enum size_t maxSizeRest = maxSizeOf!(T[1 .. $]);
>          enum size_t maxSizeOf = firstSize >= maxSizeRest ? firstSize : 
> maxSizeRest;
>      }
> }
> 
> in a non-recursive way?

Boring in D. :p

template maxSizeOf(T...) {
   enum maxSizeOf = compute();

   auto compute() {
     size_t result;
     static foreach (t; T) {
       if (t.sizeof > result) {
         result = t.sizeof;
       }
     }
     return result;
   }
}

void main() {
   pragma(msg, maxSizeOf!(double, char, string));
}

Ali



More information about the Digitalmars-d-learn mailing list