比较来自世界各地的卖家的域名和 IT 服务价格

是否可以使用原型功能 typedef 在功能的定义中?

说,我有许多函数,比如


int func1/int a, int b/ {
// ...
}
int func2/int a, int b/ {
// ...
}
// ...


现在我想简化他们的定义和公告。 我当然可以使用这样的宏:


#define SP_FUNC/name/ int name/int a, int b/


但我想保持它 C, 所以我试图使用存储说明符
typedef

为了这:


typedef int SpFunc/int a, int b/;


宣言似乎很好:


SpFunc func1; // compiles


但不确定:


SpFunc func1 {
// ...
}


是什么让我下面的错误:


error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token


有没有办法做对或是不可能的?
在我的理解中 C 它应该工作,但不起作用。

注意 gcc 了解我想要做什么,因为如果我写的话


SpFunc func1 = { /* ... */ }


它告诉我


error: function 'func1' is initialized like a variable


这意味着 gcc 它理解SPFUNC是函数的类型。
已邀请:

卫东

赞同来自:

您无法使用函数 typedef 对于功能类型。 这显然是禁止的 - 厘米。 6.9.1/2 和适当的脚注:

在定义功能中声明的标识符 /这是函数的名称/, 它应该是
具有在拒绝函数的定义中指定的功能类型。

目标是,函数定义中的类型类别不能继承 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

诸葛浮云

赞同来自:

A
typedef

确定

一种

, 不是标题 /这是源代码的文本/. 你必须使用
#define

/虽然我不建议这样做/, 如果您需要分解标题的代码。

/[Edited] 原因 , 通过哪个工作,它没有定义原型 - 它确定定义的类型变量
typedef

, 这不是你想要的。/

要回复问题请先登录注册