自参照类型 [英] Self referential type

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

问题描述

什么类型T使下列代码可编译?

What type T makes the following code compilable?

T f(){ return &f; }



我更喜欢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天全站免登陆