有人可以解释下面的代码吗?当我试图理解isNumeric时,我感到很困惑!在这种情况下,T会起作用.
auto foo(T)(T n) if (isNumeric!T) {
return (T m) {return m > n;};
}
void main() {
auto hoo5 = foo!int(1000);
writeln(hoo5(93));
writeln(hoo5(23));
}
Run Code Online (Sandbox Code Playgroud) 我在struct中有动态数组,使用动态数组的方法.问题是我运行程序时出现范围违规错误.但是当我在方法中创建一个新的动态数组时,它工作正常.以下代码会导致问题.
struct MyStr {
int[] frontArr;
this(int max = 10) {
frontArr = new int[10];
}
void push(int x) {
frontArr[0] = x;
}
}
void main() {
MyStr s;
s.push(5);
}
Run Code Online (Sandbox Code Playgroud)
但是,这一个有效;
struct MyStr {
int[] frontArr;
this(int max = 10) {
frontArr = new int[10];
}
void push(int x) {
frontArr = new int[10]; // <---Add this line
frontArr[0] = x;
}
}
void main() {
MyStr s;
s.push(5);
}
Run Code Online (Sandbox Code Playgroud)
我基本上添加该行来测试范围.似乎在push(int x)方法中看不到初始化的FrontArr.任何解释?
提前致谢.