简体   繁体   中英

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):

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)

and the foot note 162 says:

162) The intent is that the type category in a function definition cannot be inherited from a 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. 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.

The typedef for functype (in the question above) is creating a typedef for a function prototype - not a function body. In the "C" language, the function with the function body included will not evaluate to a valid type. That is why the function body is not allowed.

  • When exact type checking is used with a function body type, the only types that functype would have the as funcdefinition .functype也有funcdefinition
  • With exact type checking, the function body data type could only point to a function with .的函数。
  • If the function body were used in the type definition, then even a function with the same prototype (but a ) will technically result in a type mismatch.)的函数在技术上也会导致类型不匹配。 The current version of "C" is strongly typed. 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.


Although it is not as useful as having a typedef with a body, you create a using this typedef - and the C++ compiler will still check the argument list;可以使用此 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.

REFERENCES:

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. For example,

int myFunc()[]

says that myFunc returns an 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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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