Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc attributes with C++ methods

Tags:

c++

gcc

powerpc

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;
}
like image 446
ShaneCook Avatar asked Jan 14 '14 14:01

ShaneCook


1 Answers

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;
}
like image 141
Mine Avatar answered Oct 10 '22 02:10

Mine