Type-safe function callback definitions
Submitted by Cody Russell
Link to original bug (#589503)
Description
It would be really cool if we could support type-safe function definitions (for things like signal callbacks) on compilers that support it. For example, in gcc you can use typeof with a little extra work to check the type of a function pointer.
#define G_DEFINE_SIGNAL_HANDLER(name, rtype, args...)
typedef struct {
rtype (signal)(args);
} name ## _signal_class;
typedef typeof(((name ## _signal_class *)0)->signal) name;
G_DEFINE_SIGNAL_HANDLER(MyCallback, void, int x, int y, int z)
MyCallback myhandler; void myhandler (int x, int y, int z) { return; }
int main (int argc, char ** argv) { return 0; }
If you compile this on gcc, it should work. But try changing the third parameter of myhandler() to char and it will generate an error:
typesafe.c:19: error: conflicting types for ‘myhandler2’ typesafe.c:18: error: previous declaration of ‘myhandler2’ was here