【发布时间】:2012-04-24 10:53:26
【问题描述】:
考虑以下代码:
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,代码也会编译。
这种行为的原因是什么,应该如何解决?
【问题讨论】:
标签: foreach immutability d