Recursive declaration of function pointer in C

后端 未结 2 1257
夕颜
夕颜 2020-11-28 13:25

I\'d like to declare a function that returns a pointer to a function of the same type.

I would like to use it to implement state machines like the one below:

<
相关标签:
2条回答
  • 2020-11-28 14:10

    It's not possible to do this in C: a function can't return a pointer to itself, since the type declaration expands recursively and never ends. See this page for an explanation: http://www.gotw.ca/gotw/057.htm

    The workaround described on the above page means returning void (*) () instead of the correctly-typed function pointer; your workaround is arguably a little neater.

    0 讨论(0)
  • 2020-11-28 14:10

    This is discussed in Herb Sutter's book More Exceptional C++, Item 32, where the answer seems to be (for C) "not without use of casts". For C++ it is possible with the usual introduction of a class to provide some extra indirection.

    0 讨论(0)
提交回复
热议问题