I wrote a simple demo(test.cpp) for my question:
#include <stdio.h>
typedef void* (*SEL)(void);
int foo(int a, int b, int c) {
return a + b + c;
}
SEL _ptr_to_foo() {
return (SEL)foo;
}
int main() {
SEL sel = _ptr_to_foo();
return 0;
}
and I compiled it with g++ test.cpp -o test
in my osx and compilers complained nothing.
But I'm confused about what happened here. As my opinion, SEL
defines function pointer which parameter is void
and returns void*
. However, function foo
should be a function which accepts three int parameters and returns an int as result. I think the function pointer of foo
should be declared as something like int (*ptr)(int, int, int) = foo;
. Why the casting in _ptr_to_foo
works? What happened here?
c-style casts are fairly lax in their type checking. You should use static_cast
instead. This fails to compile:
return static_cast<SEL>(foo);