简体   繁体   English

为什么我们不能使用类型名称定义函数?

[英]Why can't we define a function using a type name?

Any ideas why can't we define a function using type name:为什么我们不能使用类型名称定义函数的任何想法:

typedef int functype(int arg1);


functype funcdefinition {
    ;
}

But we can declare one this way:但是我们可以这样声明一个:

functype funcdeclaration;

Standard says that (C11 section 6.9.1):标准说(C11 第 6.9.1 节):

The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition 162)在函数定义中声明的标识符(即函数的名称)应具有函数类型,如函数定义的声明符部分所指定的162)

and the foot note 162 says:脚注 162 说:

162) The intent is that the type category in a function definition cannot be inherited from a typedef: 162) 目的是函数定义中的类型类别不能从 typedef 继承:

 typedef int F(void); // type F is ''function with no parameters // returning int'' F f,g; // f and g both have type compatible with FF f { /* ... */ } // WRONG: syntax/constraint error F g() { /* ... */ } // WRONG: declares that g returns a function int f(void) { /* ... */ } // RIGHT: f has type compatible with F int g() { /* ... */ } // RIGHT: g has type compatible with F F *e(void) { /* ... */ } // e returns a pointer to a function F *((e))(void) { /* ... */ } // same: parentheses irrelevant int (*fp)(void); // fp points to a function that has type FF *Fp; // Fp points to a function that has type F

Therefore,所以,

functype funcdefinition;  

declares that funcdefinition is of type functype , which is correct.声明funcdefinition的类型是functype ,这是正确的。 No need of the name of function parameters in function prototype.函数原型中不需要函数参数的名称。 In case of的情况下

functype funcdefinition { ... }    

parameter names are needed and there is no way to declare the names of function parameters and therefore it is an incorrect syntax.需要参数名称,并且无法声明函数参数的名称,因此这是一个不正确的语法。

You can define a typedef for a function prototype, but not for a function body.您可以为函数原型定义 typedef,但不能为函数体定义 typedef。

The typedef for functype (in the question above) is creating a typedef for a function prototype - not a function body. functype的 typedef(在上面的问题中)正在为函数原型创建一个 typedef - 而不是函数体。 In the "C" language, the function with the function body included will not evaluate to a valid type.在“C”语言中,包含函数体的函数将不会评估为有效类型。 That is why the function body is not allowed.这就是函数体不被允许的原因。

WHY doesn't the C language allow a type to contain a function body?为什么 C 语言不允许类型包含函数体? Because:因为:

  • When exact type checking is used with a function body type, the only types that match functype would have the same body as funcdefinition .当精确的类型检查与函数体类型使用,唯一的类型匹配functype也有同样的身体funcdefinition
  • With exact type checking, the function body data type could only point to a function with exactly the same code .通过精确类型检查,函数体数据类型只能指向具有完全相同代码的函数。
  • If the function body were used in the type definition, then even a function with the same prototype (but a different body ) will technically result in a type mismatch.如果在类型定义中使用了函数体,那么即使是具有相同原型(但不同的体)的函数在技术上也会导致类型不匹配。 The current version of "C" is strongly typed.当前版本的“C”是强类型的。 Because "C and C++ (do) not let you use one type when another type is expected.", even if the C compiler were to include function bodies in a type definition (which it does not), the compiler would have no way of resolving differences in the function body other than to report the types do not match.因为“C 和 C++(做)不允许你在需要另一种类型时使用一种类型。”,即使 C 编译器在类型定义中包含函数体(它没有),编译器也无法解决函数体中除报告类型不匹配以外的差异。


Although it is not as useful as having a typedef with a body, you CAN STILL create a function pointer using this typedef - and the C++ compiler will still check the argument list;尽管它不如带有主体的 typedef 有用,但您仍然可以使用此 typedef 创建函数指针- C++ 编译器仍将检查参数列表; see below:见下文:

typedef int functype(int arg1);

int f1(int arg1) {
    return arg1;
}
int f2(int arg1) {
    return arg1;
}
int f3(int arg1, int arg2) {
    return 0;
}
int main(int argc, char* argv[])
{
    functype* pf1;
    pf1=&f1;           /* Compiles properly */
    pf1=&f3;           /* Will not compile, as expected */
                       /* because of an argument mismatch */
    return 0;
}

In this sense, using the typedef is helpful because it avoids the function pointer syntax.从这个意义上说,使用 typedef 是有帮助的,因为它避免了函数指针语法。

REFERENCES:参考:

http://www.drtak.org/teaches/modules/0027/module/node7.html http://www.drtak.org/teaches/modules/0027/module/node7.html

When you define a function as当您将函数定义为

xxx myfunction(...) {}

you are stating that myfunction is a function that returns a xxx.你说 myfunction 是一个返回 xxx 的函数。 For example,例如,

int myFunc()[]

says that myFunc returns an int.说 myFunc 返回一个 int。 Therefore所以

functype funcdefinition() { ... }  

says that funcdefinition returns a functype, which (if your example were correct) would mean that it returns "a function that takes an integer argument and returns an int".说 funcdefinition 返回一个 functype,这(如果你的例子是正确的)意味着它返回“一个接受整数参数并返回一个 int 的函数”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM