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

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

    It's probably mostly historical reasons. typedef was a relatively late addition to C, and was tacked onto the existing language (and caused a few problems for the parsing phase of compilers).

    Also, a function definition has to define the names of the parameters, if any. A function type includes the function's return type and parameter types, but not its parameter names. For example, these:

    void (int)
    void (int x)
    void (int y)
    

    are three ways of writing the same function type. If you had:

    typedef void func_t(int);
    

    then this hypothetical definition:

    func_t some_func { }
    

    wouldn't define a name for its int parameter. I'm not sure how that could have been resolved in a reasonable manner. It would be possible, I suppose, but it was never done.

    But the bottom line is probably just that Dennis Ritchie either didn't think it was worth the effort to define how a typedef could be used in a function definition, or he simply didn't think of it.

提交回复
热议问题