考虑以下代码:
immutable struct Test {
this(int foo) { }
int opApply(int delegate(ref int) dg) {
return (0);
}
}
int main(string[] argv) {
auto set = Test(); // compiles
// auto set = Test(1); // Error: cannot uniquely infer foreach argument types
foreach (item; set) { }
return 0;
}
当Test
使用默认的无参数构造函数构建结构时,代码编译得很好,但是当我尝试使用任何其他构造函数时,我得到编译时错误。如果我注释掉foreach
,代码将编译。如果我注释掉immutable
,代码也会编译。
这种行为的原因是什么以及应该如何解决?