Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 15, 2010 Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Hello gentlemen: I'm trying to translate the newest SQLite C header to D, and I stumbled unto this gem: > void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); What's that? A function pointer that takes another function pointer as its name? I'm stuck at this and I don't know how to convert it to a D function pointer. Certainly, the inner pointer is easy: > void (* function(sqlite3_vfs*,void*, const(char) *zSymbol) xDlSym)(); But what about the outer one? I am missing something? Thanks in advance. -- Yao G. |
September 15, 2010 Re: Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yao G. | On Wed, 15 Sep 2010 17:05:24 -0400, Yao G. <yao.gomez@spam.gmail.com> wrote: > Hello gentlemen: > > I'm trying to translate the newest SQLite C header to D, and I stumbled unto this gem: >> void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); > > What's that? A function pointer that takes another function pointer as its name? I'm stuck at this and I don't know how to convert it to a D function pointer. Certainly, the inner pointer is easy: >> void (* function(sqlite3_vfs*,void*, const(char) *zSymbol) xDlSym)(); > > But what about the outer one? I am missing something? > > Thanks in advance. D supports C-style function pointers. See here: typedef char sqlite3_vfs; // 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) so I think it's a function pointer that takes those parameters and returns a function pointer that takes no parameters and returns nothing. -Steve |
September 15, 2010 Re: Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Wed, 15 Sep 2010 17:15:12 -0400, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> On Wed, 15 Sep 2010 17:05:24 -0400, Yao G. <yao.gomez@spam.gmail.com> wrote:
>
>> Hello gentlemen:
>>
>> I'm trying to translate the newest SQLite C header to D, and I stumbled unto this gem:
>>> void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
>>
>> What's that? A function pointer that takes another function pointer as its name? I'm stuck at this and I don't know how to convert it to a D function pointer. Certainly, the inner pointer is easy:
>>> void (* function(sqlite3_vfs*,void*, const(char) *zSymbol) xDlSym)();
>>
>> But what about the outer one? I am missing something?
>>
>> Thanks in advance.
>
> D supports C-style function pointers. See here:
>
> typedef char sqlite3_vfs;
>
> // 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)
BTW, not sure why the const const(char*)...
-Steve
|
September 15, 2010 Re: Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Wed, 15 Sep 2010 16:15:12 -0500, Steven Schveighoffer <schveiguy@yahoo.com> wrote: > D supports C-style function pointers. See here: > > typedef char sqlite3_vfs; > > // 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) > > so I think it's a function pointer that takes those parameters and returns a function pointer that takes no parameters and returns nothing. > > -Steve Thanks Steve! -- Yao G. |
September 16, 2010 Re: Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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! -- ... <IXOYE>< |
September 16, 2010 Re: Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | On Thu, 16 Sep 2010 10:06:24 -0400, BCS <none@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 :)
D is a huge leap ahead of C in this regard!
-Steve
|
September 16, 2010 Re: Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | On 16/09/2010 15:06, BCS 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!
Perhaps the only excuse for keeping C-style function pointer declarations in D.
But since we have htod, we could just as well use it and leave D free to get rid of this fossil that leads to a syntactic ambiguity. Except that I've just found htod has a bug meaning it rejects this code.
Stewart.
|
September 17, 2010 Re: Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Hello Steven, > On Thu, 16 Sep 2010 10:06:24 -0400, BCS <none@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 :) > The trick is that function pointers are best read from the inside out. -- ... <IXOYE>< |
September 17, 2010 Re: Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 16/09/2010 15:37, Steven Schveighoffer wrote: > On Thu, 16 Sep 2010 10:06:24 -0400, BCS <none@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. |
September 17, 2010 Re: Translation of C function pointer. | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS Wrote:
> The trick is that function pointers are best read from the inside out.
> --
All C declarations are read from inside out, postfixes take precedence, that's why you have to use braces to give pointer higher precedence. One of the earlier books by Stroustroup gives a nice monster of arrays, pointers and functions to master understanding of declarations.
|
Copyright © 1999-2021 by the D Language Foundation