I’m trying to use D’s equivalent of function pointer as a way of specifying optional functions as one field in a struct, of which I’m initializing an array of. This would be simple in C (aside from the messy syntax) but I’m stuck. This program:
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() {
}
gives me these errors:
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)
I’m using dmd for D2 on a 64-bit Linux machine.
On line 17, your use of boof is a function call with no parameters (D allows the absence of parens). What you want is to take the reference of boof with the & operator.