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

后端 未结 4 1736
爱一瞬间的悲伤
爱一瞬间的悲伤 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:19

    Let me put a few words. Consider a statement:

    typedef void F(int p1, char* p2);
    

    This statement assigns name F to a function signature void (int, char*); This is definition of an alias to the function signature. After that the statement:

    F fv;
    

    tells that there is a function fv. It has the signature that was mentioned above and it has its body somewhere. Look at the C/C++ syntax of the function definition:

    retType  funcName(params) { body }
    

    There are actually 2 names used retType and funcName. None of them are the same to the name F from the initial typedef. The name F has meaning of both names. If language would allow something like:

    F { body }
    

    this will associate body with the function type. But this leads a problem:

    The meaning of F would be not clear. Is it an "alias to the function signature" or is it a "name of the entry point into a code"?

    Plus the syntax of the last example would be weird to millions of C/C++ programmers.

提交回复
热议问题