Valhalla Legends Forums Archive | C/C++ Programming | Typedef'ing functions pointers

AuthorMessageTime
iago
What's the syntax for making a typedef for a function pointer? I remember it was different from other typedefs.

Thanks!
December 10, 2003, 7:57 PM
Skywing
[quote author=iago link=board=30;threadid=4167;start=0#msg34626 date=1071086269]
What's the syntax for making a typedef for a function pointer? I remember it was different from other typedefs.

Thanks!
[/quote]
typedef rettype (callconvopt *opt name)(paramsopt);


e.g. typedef int (__stdcall * type1)(int);
typedef void (type2)();

Note that when using type2, you'll have to declare variables like this:
type2* ptr;

Some of these may be Visual C++-specific.

#1000
December 10, 2003, 8:01 PM
iago
VS specific is fine here.

Thanks!
December 10, 2003, 8:13 PM
iago
Is there a better way to do this?

typedef bool (__fastcall *FCString)(char*);
typedef bool (__fastcall *FCStringInt)(char*, int);
typedef bool (__fastcall *FCVoid)(void*, void*);
.....

like, overloading? Or am I just going to have to make a list of what I need?

Thanks! :)
December 10, 2003, 8:59 PM
DarkMinion
That's the best way...
December 10, 2003, 9:35 PM
Kp
[quote author=iago link=board=30;threadid=4167;start=0#msg34647 date=1071089995]
Is there a better way to do this?

typedef bool (__fastcall *FCString)(char*);
typedef bool (__fastcall *FCStringInt)(char*, int);
typedef bool (__fastcall *FCVoid)(void*, void*);
.....

like, overloading? Or am I just going to have to make a list of what I need?[/quote]

You might be able to make it work by leaving out the parameter list, but then the compiler won't be able to validate that you're passing the right parameters. If you decide to make a list, I'd suggest using vi substitutions to reduce repetitive typing. ;)

Start with:
FC<type>|<arglist>
:%s/^\(.*\)|\(.*\)$/typedef bool (__fastcall *\1)(\2);/
Result:
typedef bool (__fastcall *FC<type>)(<arglist>);
December 11, 2003, 12:28 AM
Etheran
regex is sexy as hell!
December 11, 2003, 12:36 AM
iago
Or I can just add them as I need them :P
December 11, 2003, 12:46 AM
Adron
[quote author=Kp link=board=30;threadid=4167;start=0#msg34698 date=1071102500]

Start with:
FC<type>|<arglist>
:%s/^\(.*\)|\(.*\)$/typedef bool (__fastcall *\1)(\2);/
Result:
typedef bool (__fastcall *FC<type>)(<arglist>);
[/quote]

What is this weird regex syntax? You're escaping characters that you want regex to treat specially! I want regex to work like /^(.*)\|(.*)$/ .... I thought that was how I used to use them too....

December 11, 2003, 4:11 AM
Kp
[quote author=Adron link=board=30;threadid=4167;start=0#msg34749 date=1071115887]
[quote author=Kp link=board=30;threadid=4167;start=0#msg34698 date=1071102500]
Start with:
FC<type>|<arglist>
:%s/^\(.*\)|\(.*\)$/typedef bool (__fastcall *\1)(\2);/
Result:
typedef bool (__fastcall *FC<type>)(<arglist>);
[/quote]What is this weird regex syntax? You're escaping characters that you want regex to treat specially! I want regex to work like /^(.*)\|(.*)$/ .... I thought that was how I used to use them too....[/quote]

Probably so, but that's the syntax I would feed to vi / vim. Presumably the author decided to violate convention because he thought that replacing literal parentheses would be done more commonly than using them to mark out subpatterns.
December 11, 2003, 5:39 PM
wut
Vim is amazing. I love my syntax color highlighting setup, I can read/write source code twice as fast. Also great for spotting syntax errors :d
December 11, 2003, 10:12 PM
Yoni
[quote author=iago link=board=30;threadid=4167;start=0#msg34647 date=1071089995]
Is there a better way to do this?

typedef bool (__fastcall *FCString)(char*);
typedef bool (__fastcall *FCStringInt)(char*, int);
typedef bool (__fastcall *FCVoid)(void*, void*);
.....

like, overloading? Or am I just going to have to make a list of what I need?

Thanks! :)
[/quote]

#define DefFCType1(type) typedef bool (__fastcall *FC_##type)(type)
#define DefFCType2(type1, type2) typedef bool (__fastcall *FC_##type1##_##type2)(type1, type2)
typedef char* pchar;
typedef void* pvoid;
DefFCType1(pchar);
DefFCType2(pchar, int);
DefFCType2(pvoid, pvoid);
((FC_pvoid_pvoid)f)(a, b);

ewwwwwwwww
December 12, 2003, 11:36 AM

Search