D:模板约束以显示给定类型是否具有可比性

Koz*_*oss 3 templates d type-constraints

我如何为以下结构编写模板约束

struct Foo (T, U) {
}
Run Code Online (Sandbox Code Playgroud)

以表明双方TU必须使用具有可比性<?我的意思是两个Ts可以比较,<两个Us可以比较<- a T和a U可以是无法比拟的.

rco*_*rre 7

我相信这会做你要求的,尽管可能有一个更简洁的解决方案:

struct Foo (T, U) if (is(typeof(T.init < T.init) : bool) 
                   && is(typeof(U.init < U.init) : bool) 
{ }
Run Code Online (Sandbox Code Playgroud)

您可以使用模板清理一下:

enum bool isSelfComparable(T) = is(typeof(T.init < T.init) : bool);

struct Foo (T, U) if (isSelfComparable!T && isSelfComparable!U) { }
Run Code Online (Sandbox Code Playgroud)