我试图在具有尾随类型的自动函数上使用 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
;
最佳答案
似乎允许在函数声明前使用 attribute 关键字。
我在gcc
属性语法中发现的以下规则spec :
An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators in a declaration of more than one identifier using a single list of specifiers and qualifiers.
和
An attribute specifier list may appear immediately before the comma, = or semicolon terminating the declaration of an identifier other than a function definition. Such attribute specifiers apply to the declared object or function.
关于c++ - 在自动函数中放置 gcc 函数属性的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41159934/