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

GCC 4.9.1 is not like function declarations with a return type of a return type with attributes when the return type is a class.

Consider the following simplified test:

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 strange warning regarding the attribute:

argh.cpp:2:41: warning: ignoring attributes applied to class typebaroutside of definition [-Wattributes]
 auto foo() -> bar __attribute__((unused)) {return bar(5);}

Combining the declaration with the definition does not disable the warning, and this only happens when the return type is a class, it works fine with int. What's happening? Why doesn't GCC like this feature-specific declaration?

+4
source share
3 answers

, , GCC. GCC :

6.31

- , .

++. [...] , , , , .

.

[...]

:

 __attribute__((unused)) 
auto foo() -> bar ;
auto foo() -> bar { return bar(5); }

.

, = , , . .

, , , .

+2

Clang ( ) :

example.cpp:7:34: warning: 'unused' attribute ignored when parsing type
      [-Wignored-attributes]
auto foo() -> bar __attribute__((unused));
                                 ^~~~~~
1 warning generated.

GCC , , . , - :

const int f(void) { return 5 };

const .

+1

GCC , .

( ).

N3337 [dcl.fct.def]:

:

          --seq opt decl-specifier-seq opt declarator virt-specifier- seq opt function-body

...

2

D1 (--)   -- < > >

     

          - < > >   exception-specification opt attribute-specifier-seq opt trailing-return-type opt

As you can see, the type trailing-return-type is part of the declarator that appears in front of the body of the function. Try changing the code:

auto __attribute__((unused)) foo() -> bar;
+1
source

All Articles