Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: iago on December 10, 2003, 01:57 PM

Title: Typedef'ing functions pointers
Post by: iago on December 10, 2003, 01:57 PM
What's the syntax for making a typedef for a function pointer?  I remember it was different from other typedefs.  

Thanks!
Title: Re:Typedef'ing functions pointers
Post by: Skywing on December 10, 2003, 02:01 PM
Quote from: iago on December 10, 2003, 01:57 PM
What's the syntax for making a typedef for a function pointer?  I remember it was different from other typedefs.  

Thanks!
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
Title: Re:Typedef'ing functions pointers
Post by: iago on December 10, 2003, 02:13 PM
VS specific is fine here.

Thanks!
Title: Re:Typedef'ing functions pointers
Post by: iago on December 10, 2003, 02:59 PM
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! :)
Title: Re:Typedef'ing functions pointers
Post by: DarkMinion on December 10, 2003, 03:35 PM
That's the best way...
Title: Re:Typedef'ing functions pointers
Post by: Kp on December 10, 2003, 06:28 PM
Quote from: iago on December 10, 2003, 02:59 PM
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?

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>);
Title: Re:Typedef'ing functions pointers
Post by: Etheran on December 10, 2003, 06:36 PM
regex is sexy as hell!
Title: Re:Typedef'ing functions pointers
Post by: iago on December 10, 2003, 06:46 PM
Or I can just add them as I need them :P
Title: Re:Typedef'ing functions pointers
Post by: Adron on December 10, 2003, 10:11 PM
Quote from: Kp on December 10, 2003, 06:28 PM

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

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....

Title: Re:Typedef'ing functions pointers
Post by: Kp on December 11, 2003, 11:39 AM
Quote from: Adron on December 10, 2003, 10:11 PM
Quote from: Kp on December 10, 2003, 06:28 PM
Start with:
FC<type>|<arglist>
:%s/^\(.*\)|\(.*\)$/typedef bool (__fastcall *\1)(\2);/
Result:
typedef bool (__fastcall *FC<type>)(<arglist>);
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....

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.
Title: Re:Typedef'ing functions pointers
Post by: wut on December 11, 2003, 04:12 PM
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
Title: Re:Typedef'ing functions pointers
Post by: Yoni on December 12, 2003, 05:36 AM
Quote from: iago on December 10, 2003, 02:59 PM
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! :)

#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