TL;DR
Is it possible, for a typedef function of type foo to point to another type bar and so on? In my experience yes, but how is this?
Preface
Inspired by this question, I tried to play around with typedef function pointers.
Something really interesting came out. I have heard that it's not possible to cast function type with typedef.
This is definitely true in cases such as:
typedef existing type sometypedef(/*parameters*/);
sometypedef testfunc{
^
/*Error*/
};
This not only because it is impossible, but also completely useless because of template, but not always...
When the magic comes in
Pointers can be very handy and lead to great confusion, but really helpful when used right. Consider the following example:
We have a function foo, that we want to refer as a pointer.
No problem's as long as no complicated parameters.
But now want to refer both foo, and other variable's inside function bar's parameters. Now we're stuck, because
void* foo(/*parameters*/);
void bar(foo* f)
or anything like that is not allowed. We simply cannot refer to foo. But don't give up yet! Because here typedef comes useful. We can make something like
typedef void (*newfunctype)(/*pointer parameters*/);
newfunctype* foo();
newfunctype bar(newfunctype* nf);
and now it works!!
...But this was not the actual question
Because the real question is how? How, for example the following piece of code works?:
typedef void (*sometype)();
typedef sometype(*anothertype)(sometype);
typedef anothertype(*yetanothertype)(sometype);
yetanothertype d(anothertype);
/*etc, other initializations*/
We can even make functions with that stuff, like
anothertype anotherfunc(anothertype* an, yetanothertype* sn);
and they... work like charm?
How long can these pointers to new typedef types be expanded? Forever?
Is this really standard c++ (or c?) syntax, or does it lead to undefined behavior?