GCC 4.9.1 does not appear to like function declarations with a trailing return type with attributes when the return type is a class.
Consider this following simplistic testcase:
struct bar
{
int a;
bar (int a) : a(a) {}
};
auto foo() -> bar __attribute__((unused));
auto foo() -> bar { return bar(5); }
int main()
{
return 0;
}
GCC prints a bizarre warning in regards to the attribute:
argh.cpp:2:41: warning: ignoring attributes applied to class type ‘bar’ outside of definition [-Wattributes]
auto foo() -> bar __attribute__((unused)) {return bar(5);}
Merging the declaration with the definition does not silence the warning, and this only happens when the return type is a class-type, it works fine with int
. What is going on? Why does GCC not like this particular function declaration?
Seems to be a bug in the attribute parser used by GCC. The GCC manual warns about potential problems with the attribute grammar :
6.31 Attribute Syntax
Because of infelicities in the grammar for attributes, some forms described here may not be successfully parsed in all cases.
There are some problems with the semantics of attributes in C++. [...] For example, there are no manglings for attributes, although they may affect code generation, so problems may arise when attributed types are used in conjunction with templates or overloading.
A warning about attribute parsing after a trailing return type would be helpful too.
An attribute specifier list may appear immediately before a declarator [...]
You should try to place the attribute before the prototype :
__attribute__((unused))
auto foo() -> bar ;
auto foo() -> bar { return bar(5); }
and it should be OK without any warning.
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.
I guess that positioning the function attribute after the function declaration is OK unless there is a trailing return type.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments