将GCC功能属性放置在自动功能中

Where to place gcc function attribute in auto function

本文关键字:功能 GCC 属性      更新时间:2023-10-16

我正在尝试使用带有尾随类型的自动函数上的GCC函数属性,但是GCC编译器一直拒绝代码。我正在基于GCC属性的位置在此处的示例上:

https://gcc.gnu.org/onlinedocs/gcc-4.3.5/gcc/function-attributes.html

  // ok
  int my_int_gcc_func ()
  __attribute__(( abi_tag ("tag1") , weak )) //ok
  ;
  // error
  auto my_auto_gcc_func_not_working ()
  __attribute__(( abi_tag ("tag2") , weak )) // error
  -> int
  // cant place attribute here, get different warning..
  ;

参考文档立即将属性放在函数参数之后。

我注意到我可以在整个函数声明前移动属性关键字,但找不到任何正式规范,说明这是GCC允许/支持的...

  // ok
  __attribute__(( abi_tag ("tag2") , weak )) // seems ok
  auto my_auto_gcc_func_no_error ()
  -> int
  ;

似乎允许在函数声明前使用属性关键字。

我在gcc属性语法规范中找到的以下规则:

使用单个指定符和预选赛列表的声明列表中,在逗号分隔的声明列表中,可能会立即出现在声明器(除第一个)列表中。

属性指定列表可能会立即出现在逗号=或半隆终止函数定义以外的标识符声明。此类属性指定符适用于声明的对象或函数。