Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates D:显示给定类型是否可比较的模板约束_Templates_D_Type Constraints - Fatal编程技术网

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))
{...}