Why can't a typedef of a function be used to define a function?

后端 未结 4 1734
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 04:47

From § 8.3.5.11 of ISO/IEC 14882:2011(E):

A typedef of function type may be used to declare a function but shall not be used to define a function

<
4条回答
  •  情深已故
    2020-12-10 05:12

    Though this question is about C++, but since C++ inherits typedef and function pointer from C, so an explanation of the same question in C can be used in here. There's a formal explanation for C.

    Rationale for International Standard - Programming Languages C §6.9.1 Function definitions

    An argument list must be explicitly present in the declarator; it cannot be inherited from a typedef (see §6.7.5.3). That is to say, given the definition:

    typedef int p(int q, int r);
    

    the following fragment is invalid:

    p funk // weird
    { return q + r ; }
    

    Some current implementations rewrite the type of, for instance, a char parameter as if it were declared int, since the argument is known to be passed as an int in the absence of a prototype. The Standard requires, however, that the received argument be converted as if by assignment upon function entry. Type rewriting is thus no longer permissible.

提交回复
热议问题