我在DMD行爲中發現了一些我不明白的東西。在班級方法中使用代表
我的代碼如下所示:
class C1 {
private static fun(alias f)() {
;
}
public static void run() {
auto f = delegate bool(int x) {return true;} ;
fun!(f)();
return;
}
}
而編譯器寫一個錯誤:
Error: template instance fun!(f) cannot use local 'f' as parameter to non-global template fun(alias f)()
所以我要創造一流的委託字段使它全球:
class C1 {
private static fun(alias f)() {
;
}
private static bool delegate(int) f;
public static void run() {
f = delegate bool(int x) {return true;} ;
fun!(f)();
return;
}
}
此代碼編譯時沒有錯誤。但我不明白,爲什麼我們不能在方法中聲明委託?
你的目標是什麼?我認爲可能有更好的方法來做到這一點。基本上'f'是一個可變的局部指向委託的指針(並且委託本身有一個上下文指針),所以你不能將它作爲模板參數傳遞 - 在編譯時必須知道這些參數。 –