13

In GCC with a C++ method defined in a header file, is it possible to use the attribute syntax? Can someone provide an example for me please. The following code does not work:

class foo
{
    public:
        void my_func() __attribute__((hot));
        void my_func()
        {
            // Some stuff
        }
};

It seems like you have to put the attributes in the declaration and not in the definition of a function. When you define a method/function in a header file you don't have a separate declaration.

Also how to use this with templates. For example the following code fails to compile with 'error: attributes are not allowed on a function-definition'.

/// Template version of max for type T
template <typename T>
inline T max(const T x, const T y) __attribute((const))
{
    if (x > y)
        return x;
    else
        return y;
}
NULL
  • 313
  • 1
  • 12
ShaneCook
  • 315
  • 1
  • 2
  • 13

3 Answers3

15

It looks like you may need to move the attribute to before the function name. On GCC 4.6.3, your code does not compile, but below code compiles.

template <typename T>
inline T __attribute__((const)) max(const T x, const T y)
{
    if (x > y)
        return x;
    else
        return y;
}
Mine
  • 4,123
  • 1
  • 25
  • 46
2

The following works (g++ 4.6.3):

class foo
{
    public:
        void my_func() __attribute__((hot))
        {
            // Some stuff
        }
};
  • You must not use a separate declaration
  • You need to remove the trailing underscores as mentioned by @Joachim

Example:

class foo {
public:
  void my_func() __attribute__((deprecated)) {
  }

  void my_func2() __attribute__((noinline)) {
  }
};

int main() {
  foo f;
  f.my_func();
  f.my_func2();
  return 0;
}
$ g++ -c -Wall -pedantic a.cpp
a.cpp: In function int main():
a.cpp:12:13: warning: void foo::my_func() is deprecated (declared at a.cpp:3) [-Wdeprecated-declarations]
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Yes, this works with the 'hot' attribute, but not with the 'noinline' attribute for some reason? It produces the error 'noinline' was not declared in this scope. The issue is I'm using -Winline flag to tell me which functions are not inlined when using -O3. It then lists certain functions, which I'd not really want inlined anyway, as not being inlined. Thus I was trying to add the noinline attribute to tell the compiler not to inline this function and thus turn off the warning. – ShaneCook Jan 15 '14 at 13:45
  • Works for me - see my updated example. Which g++ version are you using? – Andreas Fester Jan 15 '14 at 13:52
  • Using 4.6.3 for powerpc. – ShaneCook Jan 15 '14 at 14:18
0

I find that putting the attribute after the template<> and before any decorators on the function definition works best. This compiles with arm gcc and arm clang along with MacOS clang. And I don't need separate declarations anymore. This was also my only option with a templated class' templated function using arm gcc.

This worked inside and outside of a class with and without templates.

class foo {
    public:
        __attribute__((pure)) void my_func()
        {
            // Some stuff
        }
        template <typename T>
        __attribute__((pure)) void my_func(T t)
        {
            // Some stuff
        }
};

template <typename T>
__attribute((const)) inline T max(const T x, const T y) {
    if (x > y)
        return x;
    else
        return y;
}
nachum
  • 567
  • 9
  • 17