GCC attribute warning with trailing return type when return type is a class

Teemu Piippo

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?

Nicolas

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

deduce of argument of type class method (overloads by const qualifier) fails with trailing return type in gcc, but not in clang

From Dev

Trailing Return Type on operator* In Template Class

From Dev

Trailing return type and rvalues

From Dev

Trailing return type array

From Dev

Trailing return type usage when using CRTP

From Dev

Specialize auto trailing return type

From Dev

How to use trailing return type with a templated class member

From Dev

Type-id ambiguity in trailing return type

From Dev

Trailing return type issue when using restricted function

From Dev

Return class type in TypeScript

From Dev

return type is a variable in a class

From Dev

Using a function pointer with a trailing return type

From Java

Trailing return type in non-template functions

From Dev

Should main with trailing return type be avoided?

From Dev

Name lookup issue in trailing return type

From Dev

Name resolution for recursive trailing return type

From Dev

Specialize function template with decltype trailing return type

From Dev

Specialize function template with decltype trailing return type

From Dev

Should main with trailing return type be avoided?

From Dev

Warning with automatic return type deduction: why do we need decltype when return defines the type anyway?

From Java

What is the type of an 'auto' return type when returning *this in an anonymous class?

From Dev

Return class type and have class type as parameter

From Dev

GCC: warning on unused return

From Dev

GCC: warning on unused return

From Dev

Conflicting return type in implementation of 'supportedInterfaceOrientations': - Warning

From Dev

Unexpected compiler warning for "<T extends ...>" return type

From Dev

Tableview height for row return type warning

From Dev

Overriding method with generic return type with no warning

From Dev

Class Template and Reference Return Type

Related Related

  1. 1

    deduce of argument of type class method (overloads by const qualifier) fails with trailing return type in gcc, but not in clang

  2. 2

    Trailing Return Type on operator* In Template Class

  3. 3

    Trailing return type and rvalues

  4. 4

    Trailing return type array

  5. 5

    Trailing return type usage when using CRTP

  6. 6

    Specialize auto trailing return type

  7. 7

    How to use trailing return type with a templated class member

  8. 8

    Type-id ambiguity in trailing return type

  9. 9

    Trailing return type issue when using restricted function

  10. 10

    Return class type in TypeScript

  11. 11

    return type is a variable in a class

  12. 12

    Using a function pointer with a trailing return type

  13. 13

    Trailing return type in non-template functions

  14. 14

    Should main with trailing return type be avoided?

  15. 15

    Name lookup issue in trailing return type

  16. 16

    Name resolution for recursive trailing return type

  17. 17

    Specialize function template with decltype trailing return type

  18. 18

    Specialize function template with decltype trailing return type

  19. 19

    Should main with trailing return type be avoided?

  20. 20

    Warning with automatic return type deduction: why do we need decltype when return defines the type anyway?

  21. 21

    What is the type of an 'auto' return type when returning *this in an anonymous class?

  22. 22

    Return class type and have class type as parameter

  23. 23

    GCC: warning on unused return

  24. 24

    GCC: warning on unused return

  25. 25

    Conflicting return type in implementation of 'supportedInterfaceOrientations': - Warning

  26. 26

    Unexpected compiler warning for "<T extends ...>" return type

  27. 27

    Tableview height for row return type warning

  28. 28

    Overriding method with generic return type with no warning

  29. 29

    Class Template and Reference Return Type

HotTag

Archive