C语言 可以在函数定义中使用函数原型 typedef 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4574985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a function prototype typedef be used in function definitions?
提问by bitmask
I have a series of functions with the same prototype, say
我有一系列具有相同原型的函数,比如
int func1(int a, int b) {
// ...
}
int func2(int a, int b) {
// ...
}
// ...
Now, I want to simplify their definition and declaration. Of course I could use a macro like that:
现在,我想简化它们的定义和声明。当然,我可以使用这样的宏:
#define SP_FUNC(name) int name(int a, int b)
But I'd like to keep it in C, so I tried to use the storage specifier typedeffor this:
但是我想将它保留在 C 中,因此我尝试为此使用存储说明符typedef:
typedef int SpFunc(int a, int b);
This seems to work fine for the declaration:
这似乎适用于声明:
SpFunc func1; // compiles
but not for the definition:
但不是定义:
SpFunc func1 {
// ...
}
which gives me the following error:
这给了我以下错误:
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
Is there a way to do this correctly or is it impossible? To my understanding of C this should work, but it doesn't. Why?
有没有办法正确地做到这一点还是不可能?根据我对 C 的理解,这应该有效,但它没有。为什么?
Note, gcc understands what I am trying to do, because, if I write
注意,gcc 理解我想要做什么,因为,如果我写
SpFunc func1 = { /* ... */ }
it tells me
它告诉我
error: function 'func1' is initialized like a variable
Which means that gcc understands that SpFunc is a function type.
这意味着 gcc 理解 SpFunc 是一个函数类型。
回答by Johannes Schaub - litb
You cannot define a function using a typedef for a function type. It's explicitly forbidden - refer to 6.9.1/2 and the associated footnote:
不能使用函数类型的 typedef 定义函数。这是明确禁止的 - 请参阅 6.9.1/2 和相关的脚注:
The identi?er declared in a function de?nition (which is the name of the function) shall have a function type, as speci?ed by the declarator portion of the function definition.
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 F F 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 F F *Fp; //Fp points to a function that has type F
在函数定义中声明的标识符(即函数的名称)应具有函数类型,如函数定义的声明符部分所指定。
目的是函数定义中的类型类别不能从 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 F F 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 F F *Fp; //Fp points to a function that has type F
回答by user541686
A typedefdefines a type, not a header (which is source code text). You have to use #define(although I don't recommend it) if you need to factor out the code for the header.
Atypedef定义了类型,而不是标题(这是源代码文本)。#define如果您需要分解标题的代码,则必须使用(尽管我不推荐它)。
([Edited] The reason the first one works is that it's not defining a prototype -- it's defining a variable of the type defined by the typedef, which isn't what you want.)
([编辑] 第一个工作的原因是它没有定义原型——它定义了一个由 定义的类型的变量typedef,这不是你想要的。)

