Reputation: 547
got a question from adream307, I have no idea, what about yours?
I want to declare a function like this: (we named this type of function as F)
- the return type of F is "void"
- the parameter of F is a function pointer, this pointer point to a function whose type is the same as F
can i declare a function like this?
Upvotes: 3
Views: 239
Reputation: 507185
No you cannot. The type cannot be expressed, since it would repeat itself:
void f(void g(void h(...
But you can write a function which accepts itself, without any problems. Consider
void f(void g()) { }
int main(void) { f(f); }
That's perfectly fine. The parameter type of f
is a function pointer (void g()
is equivalent to void (*g)()
here) whose type is compatible with the type of f
. The rule for compatibility for the function types of both the parameter of f
and the argument in the call, void()
and void (void())
is specified as:
If one type has a parameter type list [the call argument] and the other type is specified by a function declarator that is not part of a function definition and that contains an empty identifer list [the function parameter type], the parameter list shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the application of the default argument promotions.
Both types satisfy this compatibility rule, so the function call is well defined.
Upvotes: 7