• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Templated function inlining
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Templated function inlining


  • Subject: Templated function inlining
  • From: Jonny Taylor <email@hidden>
  • Date: Sun, 23 Nov 2008 11:26:41 +0000
  • Authenticated-sender:

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...

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.
 _______________________________________________
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

  • Follow-Ups:
    • Re: Templated function inlining
      • From: Philip Aker <email@hidden>
  • Prev by Date: Re: Nothing (reliably) helpful from atos + dSYM
  • Next by Date: Re: Templated function inlining
  • Previous by thread: Re: Xcode 3.1.2 Release Notes (Chris Espinosa)
  • Next by thread: Re: Templated function inlining
  • Index(es):
    • Date
    • Thread