Re: Templated function inlining
Re: Templated function inlining
- Subject: Re: Templated function inlining
- From: Philip Aker <email@hidden>
- Date: Sun, 23 Nov 2008 04:11:59 -0800
On 2008-11-23, at 03:26:41, Jonny Taylor wrote:
This is a fairly generic C++ question; apologies if that makes it
off-topic.
I have a function which I believe should be inlined for performance,
but it isn't being inlined (probably because it looks too big). I
would like to use the always_inline attribute on it, but I can't
work out how to do that without encountering compiler errors,
because the function is a template.
My code, stripped down to a skeleton, looks like this:
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...
Using the following placement compiles without errors on my setup (g++
from Terminal):
template<class Type> class MyClass {
__attribute__((always_inline)) int LowestCost1( void ) const {
return Operator1<Type>::Cost();
}
template<class Operator> __attribute__((always_inline)) int
LowestCost2( void ) const {
return Operator::Cost();
}
Philip Aker
echo email@hidden@nl | tr a-z@. p-za-o.@
Democracy: Two wolves and a sheep voting on lunch.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden