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;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With