Translation of C function pointer.
Stewart Gordon
smjg_1998 at yahoo.com
Thu Sep 16 19:51:56 PDT 2010
On 16/09/2010 15:37, Steven Schveighoffer wrote:
> On Thu, 16 Sep 2010 10:06:24 -0400, BCS <none at anon.com> wrote:
>
>> Hello Steven,
>>
>>> // note you can't use void as a parameter type in D
>>> void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(/*void*/);
>>> pragma(msg, typeof(xDlSym).stringof);
>>> outputs:
>>> void function() function(sqlite3_vfs*, void*, const const(char*)
>>> zSymbol)
>>
>> D, now with C type un-garbleing!
>
> I'd have to say, if I wasn't able to use D to do this, it would have
> taken me hours to figure this one out. Even knowing what it is now, I
> still can't read it :)
<snip>
It took me a moment as well. Basically, the C declaration
ReturnType (*Name)(Parameters);
becomes in D
ReturnType function(Parameters) Name;
Where you've got one inside another, you try to apply the same
principle. So
void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
starting with the outermost level, you end up with (note (void) becomes ())
void function() (*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol);
instead of Name, you've got this funny thing. You're left with a
function pointer declaration where void function() is the ReturnType, so
transforming it again you get
void function() function(sqlite3_vfs*, void*, char* zSymbol) xDlSym;
or in D2,
void function() function(sqlite3_vfs*, void*, const(char)* zSymbol) xDlSym;
Whoever wrote the C declaration must have had an even harder job getting
it right! I wouldn't have hesitated to use a typedef, if ever I had
reason to do C stuff as complicated as this.
Stewart.
More information about the Digitalmars-d-learn
mailing list