我确定我在这里遗漏了一些明显的东西——D 的其余部分(甚至是编译器错误)非常明智且易于理解。我有一个std.containers.Array
可比较的结构,我想对其进行排序。文档指出,std.containers
为了使用其中的内容,std.algorithm
您需要使用array[]
或对其进行切片array.opSlice()
。好的没问题。
但是,如果我Array
选择两种非常琐碎的类型,它不会排序 - 相反,它告诉我 Phobos 深处的例程不是 nothrow (?)
B:\lib\D\dmd2\windows\bin\..\..\src\phobos\std\range\package.d(7189): Error: 'std.range.SortedRange!(RangeT!(Array!(MyInt)), "a < b").SortedRange.dbgVerifySorted' is not nothrow
B:\lib\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\sorting.d(982): Error: template instance std.range.assumeSorted!("a < b", RangeT!(Array!(MyInt))) error instantiating
main.d(21): instantiated from here: sort!("a < b", cast(SwapStrategy)0, RangeT!(Array!(MyInt)))
下面的最小示例。第一个sort
(自动生成的两个值的标准数组)排序很好。其他sort
调用因上述编译器错误而失败。使用 VS Community 2015 的 DMD2 构建,我找不到编译器版本标识符,但这是昨天才下载的。
import std.array;
import std.container.array;
import std.algorithm.sorting;
struct MyInt
{
int data;
int opCmp(MyInt o)
{
return data - o.data;
}
}
int main(string[] argv)
{
MyInt ami, bmi;
Array!MyInt arr = [ ami, bmi ];
sort([ami, bmi]);
sort(arr[0..2]);
sort(arr[]);
sort(arr.opSlice());
return 0;
}