An interesting problem, but without an acceptable solution to C, I think.
Why is this not possible with C? (some specs here)
Return function type T:
T (*)(void) ;
Which expects T to be defined, of course ... But then, since T is a type of the function itself, there is a circular dependence.
For structure T, we could have:
struct T ; typedef T * (*f)(void) ;
Were the following notation convenient?
function T ; TT(void) ;
But since there is no way to forward-declare a function, then there is no way to use the construct that you wrote in your question.
I am not a compiler lawyer, but I believe that this circular dependency is created only because of the typedef record, and not because of the C / C ++ restriction. In the end, function pointers (I'm talking about functions here, not object methods) are the same size (just like structure or class pointers are the same size).
Learning C ++ Solution
As for C ++ solutions, the previous answers gave good ones (I think of zildjohn01 answer , here).
An interesting point is that they are all based on the fact that structures and classes can be declared ahead (and are considered forward declared in their declaration):
#include <iostream> class MyFunctor { typedef MyFunctor (*myFunctionPointer)() ; myFunctionPointer m_f ; public : MyFunctor(myFunctionPointer p_f) : m_f(p_f) {} MyFunctor operator () () { m_f() ; return *this ; } } ; MyFunctor foo() { std::cout << "foo() was called !" << std::endl ; return &foo ; } MyFunctor barbar() { std::cout << "barbar() was called !" << std::endl ; return &barbar ; } int main(int argc, char* argv[]) { foo()() ; barbar()()()()() ; return 0 ; }
What outputs:
foo() was called ! foo() was called ! barbar() was called ! barbar() was called ! barbar() was called ! barbar() was called ! barbar() was called !
Inspiration from a C ++ solution to achieve a C solution
Could you use a similar method in C to achieve comparable results?
Somehow, yes, but the results are not as sexy as C ++ - solution:
#include <stdio.h> struct MyFuncWrapper ; typedef struct MyFuncWrapper (*myFuncPtr) () ; struct MyFuncWrapper { myFuncPtr f ; } ; struct MyFuncWrapper foo() { printf("foo() was called!\n") ; /* Wrapping the function */ struct MyFuncWrapper w = { &foo } ; return w ; } struct MyFuncWrapper barbar() { printf("barbar() was called!\n") ; /* Wrapping the function */ struct MyFuncWrapper w = { &barbar } ; return w ; } int main() { foo().f().f().f().f() ; barbar().f().f() ; return 0 ; }
What outputs:
foo() was called! foo() was called! foo() was called! foo() was called! foo() was called! barbar() was called! barbar() was called! barbar() was called!
Conclusion
You will notice that the C ++ code is very similar to the C code: each source will use the structure as a container for the function pointer, and then use the pointer contained in the callback if necessary. Of course, the C ++ solution uses operator () overloading, makes characters private, and uses a specific constructor as syntactic sugar.
(Here's how I found a C solution: trying to reproduce C ++ - a "manual" solution)
I don't believe that we could make better use of syntactic sugar C using macros, so we stick to this C solution, which I find far from impressive, but still wondering how long it took me to find it.
After all, finding solutions to bizarre problems is a surefire way to find out ...
:-)