递归返回类型
我刚刚浏览了 Mark Probst 的文凭论文,并偶然发现了以下代码:
typedef void* cont(void);
for (;;)
{
cp = (cont*)(*cp)();
}
我很确定演员应该阅读 (cont)
,而不是 (cont*)
,因为他解释道:
希望进行正确尾部调用的函数返回要调用的函数的地址
,并且 cont 已经是函数指针类型。因此,让我们将该行更改为:
cp = (cont)(*cp)();
现在我想知道,我们如何摆脱演员阵容?是否可以定义 cont
以使其返回 cont
? cont
的 typedef
是什么样子的?我们需要一个辅助类型来实现这一点吗?难道不可能吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,
typedef void* cont(void);
cont
定义返回void *
的函数类型。我认为您将其与typedef void (*cont)(void);
或typedef void *(*cont)(void);
混淆了。我不认为在这种情况下可以消除对演员阵容的需要,但我愿意相信其他情况。
No,
typedef void* cont(void);
cont
defines a function type returning avoid *
. I think you are confusing it withtypedef void (*cont)(void);
ortypedef 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.
cont
类型是一个返回void
指针的函数。因此cont *
是指向此类函数的指针,并且*
无法从强制转换中删除。The type
cont
is a function returning avoid
pointer. Thereforecont *
is a pointer to such a function, and the*
cannot be removed from the cast.