2011-11-18 74 views
1

我試圖使用D的等價函數指針作爲一種方式來指定可選函數作爲結構中的一個字段,其中我正在初始化一個數組。這在C中很簡單(除了混亂的語法外),但我被卡住了。這個程序:編譯器錯誤與D的等價函數指針

struct Foo { 
    ubyte code; 
    bool yup; 
    void function(ubyte[] data, int size) special; 
} 

void boof(ubyte[] data, int size) { 
    /*do something*/ 
} 

static immutable Foo[] markdefs = [ 
    {0xF2, true, null}, 
    {0xE4, true, boof}, 
    {0xEE, false, null} 
]; 

void main() { 
} 

給了我這些錯誤:

funptr.d(17): Error: function funptr.boof (ubyte[] data, int size) is not 
callable using argument types() 
funptr.d(17): Error: expected 2 function arguments, not 0 
funptr.d(17):  called from here: boof() 
funptr.d(17): Error: cannot implicitly convert expression (boof()) of type 
void to void function(ubyte[] data, int size) 

我使用DMD爲D2 64位Linux機器上。

回答

4

在第17行,您使用boof是一個不帶參數的函數調用(D允許不存在parens)。你想要的是參考boof&運營商。