A typedef that refers to itself

前端 未结 2 1426
一个人的身影
一个人的身影 2021-01-11 10:44
typedef bool (*Foo)(Foo a, Foo b);

How do you declare a function pointer that accepts itself in its parameters?

2条回答
  •  暖寄归人
    2021-01-11 11:01

    You can't express this in the type system. Although there's nothing fundamentally wrong in this, it simplifies things by eliminating cycles within the type system. It strongly reminds me of Axiom of Foundation.

    What you can do is pass a void pointer and cast it to your type:

    typedef bool (*Foo)(void* a, void* b);
    
    bool f(void* a, void* b)
    {
        return ((Foo)a)(a,b);
    }
    

提交回复
热议问题