Bug 60625 - attributes on template member function definition inside class definition not supported
Summary: attributes on template member function definition inside class definition not...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2014-03-23 16:22 UTC by petschy
Modified: 2021-12-10 09:50 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-12-10 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description petschy 2014-03-23 16:22:33 UTC
struct Foo
{
        template<int U>
        // error: attributes are not allowed on a function-definition
        static int Bar()  __attribute__((always_inline))
        {
                return U;
        }

        // no error, although this is a fn definition, too
        static int Baz(bool x) __attribute__((always_inline))
        {
                return x ? Bar<5>() : Bar<42>(); 
        }
};

g++ -c 20140323-force_inline.cpp 
20140323-force_inline.cpp:5:20: error: attributes are not allowed on a function-definition
  static int Bar()  __attribute__((always_inline))

Tried with all minor versions from 4.4 to 4.9, same results.

Supporting attributes on in-class defined template members would be really useful, or rather, convenient. Otherwise, one have to declare the function with the attribute inside the class, then define it outside the class. For short and simple functions this is rather tedious.

Regards, Peter
Comment 1 Marc Glisse 2014-03-23 16:31:40 UTC
For function definitions (nothing to do with templates) the attribute has to come at the beginning, not at the end. Move it right before 'static' and it will compile.
Comment 2 Andreas Schwab 2014-03-23 16:40:15 UTC
Or somewhere else before the function name, ie. as part of the declaration-specifiers.
Comment 3 petschy 2014-03-23 18:04:45 UTC
Thanks. It's then an inconsistency, right? Because the non-template fn def didn't trigger the error while the template version did so.

Moreover, the error message is misleading, because it said attributes were not allowed, while they are allowed, just not at the end.
Comment 4 Andrew Pinski 2021-12-10 07:17:41 UTC
Confirmed, the non-template case should have been rejected.

Note if you use the C++11 style attribute [[gnu::always_inline]], GCC does warn about it when used after the function declaration and such.