一个typedef的功能原型 [英] typedef a functions prototype
问题描述
我有相同的原型一系列功能,比如
INT的func1(int类型的,INT B){
// ...
}
INT FUNC2(int类型的,INT B){
// ...
}
// ...
现在,我想简化他们的定义和声明。当然,我可以使用宏这样的:
的#define SP_FUNC(名称)INT名(INT A,INT B)
不过,我想保持它在C,所以我试图用存储说明的typedef
此:
的typedef INT SpFunc(int类型的,INT B);
这似乎为申报工作的罚款:
SpFunc func1的; //编译
而不是为定义:
SpFunc func1的{
// ...
}
这给了我以下错误:
错误:预期'=',',',',','ASM'或'__attribute__'前'{'令牌
有没有办法做到这一点正确与否是不可能?
我的C的理解这应该工作,但事实并非如此。为什么呢?
请注意,gcc的理解是什么,我试图做的,因为,如果我写
SpFunc FUNC1 = {/ * ... * /}
它告诉我,
错误:函数'func1的初始化像变量
这意味着,海湾合作委员会了解到,SpFunc是一个函数类型。
您不能定义使用的typedef为函数类型的函数。它明确禁止 - 参见6.9.1 / 2和相关的脚注:
的鉴定网络连接器中的函数德音响nition(这是函数名)声明应
有一个函数类型,特定网络的函数定义的声明符部分编着。
这样做的目的是,在函数定义的类型类别不能从一个typedef继承:
的typedef INT F(无效); // F型是不带参数的功能
//返回int
量F f,克; // f和g都有具有F兼容型
量F f {/ * ... * /} // WRONG:语法/约束错误
F G(){/ * ... * /} // WRONG:声明使得G返回函数
INT F(无效){/ * ... * /} // RIGHT:F的类型使用F兼容
INT克(){/ * ... * /} // RIGHT:G的类型为F兼容
F * E(无效){/ * ... * /} // e返回一个指针的函数
F *((E))(无效){/ * ... * /} //相同:括号无关
INT(* FP)(无效); // FP指向具有F型功能
F *计划生育; // FP指向具有F型功能
块引用>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
typedef
for this: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?
Note, gcc understands what I am trying to do, because, if I write
SpFunc func1 = { /* ... */ }
it tells me
error: function 'func1' is initialized like a variable
Which means that gcc understands that SpFunc is a function type.
解决方案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:
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.
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的功能原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!