函数原型 typedef 可以在函数定义中使用吗?

发布于 2024-10-10 02:54:40 字数 906 浏览 0 评论 0原文

我有一系列具有相同原型的函数,比如说

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 是一个函数类型。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青丝拂面 2024-10-17 02:54:40

您不能使用函数类型的 typedef 来定义函数。这是明确禁止的 - 请参阅 6.9.1/2 和相关脚注:

函数定义中声明的标识符(即函数的名称)应
有一个函数类型,由函数定义的声明符部分指定。

其目的是函数定义中的类型类别不能从 typedef 继承:

typedef int F(void); // F 类型是“不带参数的函数”
                     // 返回整数”
Ff,g; // f 和 g 都具有与 F 兼容的类型
F f { /* ... */ } // 错误:语法/约束错误
F g() { /* ... */ } // 错误:声明 g 返回一个函数
int f(void) { /* ... */ } // 右:f 具有与 F 兼容的类型
int g() { /* ... */ } // 右:g 的类型与 F 兼容
F *e(void) { /* ... */ } // e 返回指向函数的指针
F *((e))(void) { /* ... */ } // 相同:括号不相关
int (*fp)(无效); // fp 指向类型为 F 的函数
F*Fp; //Fp指向一个类型为F的函数

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
剧终人散尽 2024-10-17 02:54:40

typedef 定义类型,而不是标头(它是源代码文本)。如果您需要提取标头的代码,则必须使用 #define (尽管我不推荐它)。

([编辑]第一个起作用的原因是它没有定义原型 - 它定义了由 typedef 定义的类型的变量,这不是您想要的。)

A typedef defines 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.

([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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文