Generics 一般限制类型

Generics 一般限制类型,generics,max,d,html,min,Generics,Max,D,Html,Min,有没有人设计了一个通用的D结构来分组 类型和默认值的最大值在 正确的方法 差不多 alias Pair(T) = Tuple!(T, T); 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

有没有人设计了一个通用的D结构来分组 类型和默认值的最大值在 正确的方法

差不多

 alias Pair(T) = Tuple!(T, T);

 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;
     Pair!T _minmax;
 }
 auto limits(T)() { return Limits!T(); }
 unittest {
     Limits!int x;
     dln(x);
 }
我希望min和max默认初始化为T.max,T.min,以便 为
x=min/max(x,…)
逻辑做好准备。但是代码 上面的操作不起作用,因为没有调用init()函数 不知道为什么。我不能使用默认成员初始化 因为我希望
限制
也能与
SysTime
只有在运行时才知道最小值和最大值

我知道std.datetime.span,但它不是通用的

有人吗

另见:

更新


我刚刚读到,当
T
SysTime
时,我应该使用
std.datetime.Interval
。这简化了事情。

不会调用
init
函数,因为您从未调用过它!除了复制初始值设定项外,D从不进行默认构造。这样做的方法是禁用this(),利用
限制!int x编译错误,强制用户调用带有参数的工厂函数或构造函数来初始化它

struct S {
     @disable this(); // disable S s; declars
     this(int min, int max) { ... } // allow S s = S(0, 10);
}