Templates D:显示给定类型是否可比较的模板约束
如何为以下结构编写模板约束Templates D:显示给定类型是否可比较的模板约束,templates,d,type-constraints,Templates,D,Type Constraints,如何为以下结构编写模板约束 struct Foo (T, U) { } 为了表明T和U必须使用进行比较,我相信这会满足您的要求,尽管可能有一个更简洁的解决方案: struct Foo (T, U) if (is(typeof(T.init < T.init) : bool) && is(typeof(U.init < U.init) : bool) { } structfoo(T,U)if(is)(typeof(T.ini
struct Foo (T, U) {
}
为了表明
T
和U
必须使用进行比较,我相信这会满足您的要求,尽管可能有一个更简洁的解决方案:
struct Foo (T, U) if (is(typeof(T.init < T.init) : bool)
&& is(typeof(U.init < U.init) : bool)
{ }
structfoo(T,U)if(is)(typeof(T.init
您可以使用模板稍微清理一下:
enum bool isSelfComparable(T) = is(typeof(T.init < T.init) : bool);
struct Foo (T, U) if (isSelfComparable!T && isSelfComparable!U) { }
enum bool isselfcompariable(T)=is(typeof(T.init
目前我能想到的最简洁的方法是
struct Foo(T, U)
if(is(typeof(T[0] < T[0])) && is(typeof(U[0] < U[0])))
{
}
但是如果您计划直接使用比较运算符,那么只需检查它们是否编译就足够了
struct Foo(T, U)
if(is(typeof(T.init < T.init)) && is(typeof(U.init < U.init)))
{
}
InputRange find(alias pred = "a == b", InputRange, Element)
(InputRange haystack, Element needle)
if (isInputRange!InputRange &&
is (typeof(binaryFun!pred(haystack.front, needle)) : bool))
{...}