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
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.
Or somewhere else before the function name, ie. as part of the declaration-specifiers.
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.
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.