template<class Type> struct Operator1 { static int Cost(void) { return 1; } };
template<class Type> class MyClass
{
// I can specify the always_inline attribute here
int LowestCost1(void) const __attribute__((always_inline))
{
return Operator1<Type>::Cost();
}
// The always_inline attribute leads to an error:
// "attributes are not allowed on a function-definition"
template<class Operator> int LowestCost2(void) const __attribute__((always_inline))
{
return Operator::Cost();
}
public:
int MemberFunc(void) const
{
int cost;
cost = LowestCost1();
cost += LowestCost2<Operator1<Type> >();
return cost;
}
};
I have encountered this error before, and have fixed it by putting the attribute on a prototype for the function. I am not sure how I can provide a prototype for a templated member function, though. Incidentally, I am slightly surprised that I get away with giving the attribute on LowestCost1, and I'm not quite sure why it works there but not on LowestCost2.
Can anybody advise on how I can force inlining(*) in this case? A portable solution would be ideal, but an Xcode-specific solution would be a great help too. I'd like to think that a round of feedback-directed optimization would help, but I'd prefer to avoid that if I can help it as this is code that's still very much under development...
Thanks in advance
Jonny
(*) I will of course measure performance to establish whether it is faster with this inlined or not, but I have good reason to expect it will be in this case.