一、简介
__attribute__ 是个编译器指令,告诉编译器声明的特性,或者让编译器进行更多的错误检查和高级优化。
attribute 可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。
__attribute__ 是GCC的特性,LLVM借鉴过来,又对其进行了扩展。
当__attribute__ 用于修饰对象时,它就如同C 语言语法体系结构的类型限定符,跟const , volatile , restrict 等属一类。
当__attribute__ 用于修饰函数时,它就相当于一个函数说明符,跟inline,Noreturn 属同一类。
当__attribute_ 用于修饰一个结构体,联合体或者枚举类型,该限定符只能放在类型标识符之前。
函数属性(Function Attribute) | 类型属性(Type Attributes) | 变量属性(Variable Attribute) | Clang特有的 |
---|---|---|---|
noreturn | aligned | alias | availability |
noinline | packed | at(address) | overloadable |
always_inline | bitband | aligned | |
flatten | deprecated | ||
pure | noinline | ||
const | packed | ||
constructor | weak | ||
destructor | weakref(“target”) | ||
sentinel | section(“name”) | ||
format | unused | ||
format_arg | used | ||
section | visibility(“visibility_type”) | ||
used | zero_init | ||
unused | |||
deprecated | |||
weak | |||
malloc | |||
alias | |||
warn_unused_result | |||
nonnull | |||
nothrow (不抛出C++ 异常) |
二、语法
__attribute__ 书写特征是:__attribute__ 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__ 参数。
attribute 语法格式为: __attribute__ ((attribute-list))
三、示例
ref:
https://www.jianshu.com/p/e2dfccc32c80