__attribute__说明符可以同时用于函数原型和函数定义吗?
Can an __attribute__ specifier be used with both the function prototype and the function definition?
我的问题直接与__attribute__((noreturn))
有关,但更普遍的是也可能与其他人有关-例如__attribute__(noinline)
。我已经查看了gcc手册和Keil编译器参考指南,以确定使用__attribute__
函数的正确语法。我所看到的大致如下:
void function (void) __attribute__((noreturn)); //Prototype has __attribute__
void function (void) //Definition does not.
{
while (1);
}
我还看到在函数定义之前使用的__attribute__
如下所示:
__attribute__((noreturn)) void function (void)
{
while (1);
}
然而,我还没有看到它与函数原型和函数定义一起使用的例子。我认为在两个位置都有__attribute__
会导致更好的代码可读性;我可以通过查看函数原型或定义来知道是否应用了属性。结果将如下所示:
__attribute__((noreturn)) void function (void) ; //Prototype has __attribute__
__attribute__((noreturn)) void function (void) //Definition has __attribute__
{ //as well.
while (1);
}
我已经使用前面提到的方法成功地用Keil armcc编译器编译了代码。有任何理由为什么我应该不使用此方法与armcc或gcc?
这里是GCC 4.0文档的一个片段。
The keyword __attribute__ allows you to specify special attributes when making a
declaration.
注意这里说的是"声明"而不是"定义"。这篇Unix Wiz的老文章也有很多好的建议。它还要求在声明中使用属性。
正如Sean Perry所说,看起来GCC只指定可以与声明一起使用的特殊属性。
我在ARMCC文档中挖掘了更多,终于找到了我要找的东西:
您可以在声明、定义或两者中设置这些函数属性。
因此,对于ARMCC 我的使用__attribute__
如OP中所示是安全的,但对于GCC来说并非如此。
相关文章:
- 函数如何通知用户它基于函数原型抛出异常?
- 在函数中拥有函数原型的目的是什么?
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以使用 libclang python 解析 cpp 文件中没有标头的函数原型
- 有没有办法在C++编译时更改函数原型?
- 省略函数原型中的返回类型
- 如何使用 "using" 关键字定义函数原型/签名
- 函数原型未初始化的局部变量
- 如何创建函数原型命名空间
- 标准库头文件函数原型的实现是如何用 c++ 编写的?
- 为什么这种类型的函数原型会引发错误?
- 函数指针类型不能用于函数原型
- 了解此函数原型
- 将字符串向量传递给函数和函数原型问题 c++
- 函数原型范围的有趣用法
- 为什么这不起作用(C++函数原型)
- 将数组传递到函数会在函数原型声明中出现错误
- 在头文件中的函数原型中获取指针数组:函数声明为 void
- 类/成员函数中的函数原型
- 函数原型中的字符串初始化