Generic Span/Limits/MinMax Type

"Nordlöw" per.nordlow at gmail.com
Mon Jan 20 11:36:12 PST 2014


Has anyone cooked up a generic D struct that groups together min
and max values of a type and default-initializes them in the
correct way?

Something like

      struct Limits(T)
      {
          /* TODO: Fix purity of this by fixing Bytes.value() */
          auto init() @trusted /* pure */ nothrow { return
tuple(T.max,

T.min); }
          alias _minmax this;
          Tuple!(T,T) _minmax;
      }
      auto limits(T)() { return Limits!T(); }
      unittest {
          Limits!int x;
          dln(x);
      }

I want min and max to default initialize to T.max, T.min so they
are prepared for x = min/max(x, ...) arithmetic. But the code
above doesn't work because the init() function isn't called and I
don't know why. And I can't use default member initialization
because I want `Limits` to work also with types such as `SysTime`
when min and max are only know at run-time.

I'm aware of `std.datetime.span` but it isn't generic.

Ideas anyone?


More information about the Digitalmars-d-learn mailing list