开发者

recursive return type

开发者 https://www.devze.com 2023-01-31 09:28 出处:网络
I just browsed through Mark Probst\'s diploma thesis and stumpled over the following code: 开发者_运维百科typedef void* cont(void);

I just browsed through Mark Probst's diploma thesis and stumpled over the following code:

开发者_运维百科typedef void* cont(void);

for (;;)
{
    cp = (cont*)(*cp)();
}

I'm pretty sure the cast should read (cont), not (cont*), because he explains:

The function wishing to do a proper tail call returns the address of the function to be called

And cont is already a pointer-to-function type. So let's change that line to:

    cp = (cont)(*cp)();

Now I was wondering, how can we get rid of the cast? Can cont be defined so it returns a cont? How would the typedef for cont look like? Do we need a helper type to achieve this? Is it impossible?


No, typedef void* cont(void); cont defines a function type returning a void *. I think you are confusing it with typedef void (*cont)(void); or typedef void *(*cont)(void);.

I don't believe that it's possible to eliminate the need for a cast in this scenario but I'm open to be convinced otherwise.


The type cont is a function returning a void pointer. Therefore cont * is a pointer to such a function, and the * cannot be removed from the cast.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号