Object.Error:打印std.algorithm.cartesianProduct的结果时发生访问冲突

Object.Error:打印std.algorithm.cartesianProduct的结果时发生访问冲突,d,phobos,D,Phobos,我正在为x86使用DMD2.062 module test; private enum test1 { one, two, three, } private enum test2 { one, two, three, } auto ct = cartesianProduct([EnumMembers!test1], [EnumMembers!test2]); unittest { import std.stdio;

我正在为x86使用DMD2.062

module test;    

private enum test1
{
    one,
    two,
    three,
}

private enum test2
{
    one,
    two,
    three,
}

auto ct = cartesianProduct([EnumMembers!test1], [EnumMembers!test2]);

unittest
{
    import std.stdio;
    foreach (n, m; ct)
    {
        writeln(n, " ", m);
    }
}
此程序打印出:

one one
two one
three one

然后抛出访问冲突错误。我是否错误地使用了cartesianProduct,或者这是函数中的一个错误?

可能两者都有一点点。这里的问题是,
ct
试图在编译时进行评估,并生成在运行时使用的结果范围。我猜CTFE或cartesianProduct都不希望出现这种情况,而且会发生一些糟糕的事情,包括使用无效内存。我认为它要么可以工作,要么是编译时错误,但这对您没有帮助,属于bug跟踪器

不过,这里重要的是,如果您将
ct
初始化移动到单元测试体或
静态this()
模块构造函数,那么一切都将工作。您似乎忽略了D不支持在程序启动时初始化全局变量。分配给全局的值总是在编译时进行计算,这通常“正常工作”,通常会导致编译时错误(如果初始化无法CTFE),在这种情况下会导致奇怪的行为:)

您可能需要以下代码:

auto Test1Members = [ EnumMembers!test1 ];
auto Test2Members = [ EnumMembers!test2 ];
alias CT = typeof(cartesianProduct(Test1Members, Test2Members));
CT ct;

static this()
{
    ct = cartesianProduct(Test1Members, Test2Members);
}
一般来说,对于复杂类型(如数组或关联数组),编译时数据和运行时数据之间的互连在当前的D前端实现中非常棘手,需要大量关注。

已经提交,请看开发人员的意见。