自我指涉类型 [英] Self referential type

查看:168
本文介绍了自我指涉类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么类型T提出以下code编译?

What type T makes the following code compilable?

T f(){ return &f; }

我preFER一个C答案,但我打上了一个问题,C和C ++中的情况下,只有使用模板的答案。

I'd prefer a C answer, but I marked the question as C and C++ in case there is only an answer using templates.

推荐答案

我希望这不是欺骗(仅限于C ++):

I hope this isn't cheating (C++ only):

class T {
private:
    T (*_func)();

public:
    T(T (*func)()) : _func(func) {}

    T operator()() {
        return *this;
    }
};

T f() { return &f; }

int main() {
    f()()()()()()()();
}

这篇关于自我指涉类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆