This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ function pointers and automatic class refrences


>I posted a follow up message that uses some evil macros
>and gets the job done, the problem is that it doesn't approach any
>sort of type safety, so I was looking for some sort of thing built
>into the C++ language that does this.

No type safety is not the only problem. I think on various targets 
this code will not work at all. (PPC for example passes regular 
parameters in register, while "..." parameters are on stack, so the 
function will look for its params in the wrong place)

>An "interesting" bit of code. . .  The only problem is that this is
>going to live in a library somewhere, so using templates is
>impossible.

well, you can for example do something like in your header:

void _my_func(void *obj, void (*func)(void *, int, int));

template<class T> void my_func(T& obj, void (T::*func)(int, int)) {
	_my_func(&obj, (void (*)(void *, int, int)) func);
}

and then implement _my_func in your library, for example:

void _my_func(void *obj, void (*func)(void *, int, int)) {
	func(obj, 1, 2);
}

it's not entirely nice, since it assumes that member function call is 
similar to regular function call with "this" as an implicit first 
parameter with a size matching that of void *.

But, at least it has type-safety.

  - xmath


Matthijs van Duin     (NOTE: PGP key has changed!)
- PGP Key: 0x2D6F0BA7
- FP: 205D F7BA FFAD 9070 AB9E  C5A8 BDB0 CA1B 2D6F 0BA7 -


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]