typedef bool (*Foo)(Foo a, Foo b);
How do you declare a function pointer that accepts itself in its parameters?
Indirectly:
struct Foo{
typedef bool (*FooPtr)(Foo a, Foo b);
Foo(FooPtr p)
: p(p)
{}
bool operator()(Foo a, Foo b) const{
return p(a,b);
}
FooPtr p;
};
struct Bar{
Bar(Foo f)
: some_callback(f)
{}
Foo some_callback;
};
bool a_callback(Foo a, Foo b){
return false;
}
int main() {
Bar b(a_callback);
b.some_callback(Foo(a_callback), Foo(a_callback));
}
Not that I could ever see any use in that, as you can see from my example.
You can't express this in the type system. Although there's nothing fundamentally wrong in this, it simplifies things by eliminating cycles within the type system. It strongly reminds me of Axiom of Foundation.
What you can do is pass a void pointer and cast it to your type:
typedef bool (*Foo)(void* a, void* b);
bool f(void* a, void* b)
{
return ((Foo)a)(a,b);
}