PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Template functions

Hi,

I've been trying to understand templates and metaprogramming for a while, but while I understand and write a few normal template classes I never got around to grasping the basics of metaprogramming.

In any case, I believe I have a case where it could be applied, but I'm not sure. Here goes.

I have a function that gets calles thousands of time. The function is mainly like this:

Code:

void Function(flag)
{
switch(flag)
{
case _this_:
do this;
break;
case _that_:
do that;
break;
}
}

the flag is defined at run-time, not design time.

Still, I believe I could get rid of the switch with a function template or some clever use of templates. This function gets called so much that any optimization would be welcome, even if it's not a bottleneck.

I've read some books on templates, but while I understand what I'm reading, I find that I'm rarely able to put it in practive.

So, is this a case where templates could help?

Thanks,

--Eric
I don't know the context that flag is used in elsewhere, but you could make it a pointer to a function, and just call it at the apropriate time.

Dan East
I think templates could be useful in that case if both functions are somewhat similar - I like this template use in ScummVM interpolation code for example
angedelamort · 05 Feb 2004, 18:13 UTC
Just use template specialization.

I'm not really sure what you try to do, but look for this topic, it should help you to optimize. If you want an example of what I'm thinking, I'll write you a little sample.

small sample of what i'm thinking:

Code:

template<int>
void function()
{
// do something generic
}


template<10>
void function()
{
// do the specializetaion
}

// ...
// call a function
function<10>();


But sending a pointer to a function should do the same thing like Dan said
i'm not sure what you mean with "the flag is defined at run-time, not design time".

generally you should keep in mind that template programming is a compile-time technique. what it does is code expansion/creation depending on the template types which have therefore to be known at compile-time.

for your problem (as far as i understand it) i propose taking dan's suggestion of using a function pointer...

bye,
Daniel
drgoldie wrote:
generally you should keep in mind that template programming is a compile-time technique. what it does is code expansion/creation depending on the template types which have therefore to be known at compile-time.



I wonder what's the advantage of templates over good old macros ?
templates provide - to some extend - type safety, but infact they are more or less the super-luxus-version of macros... :)

Daniel
This seems to be similar to what I was trying to do, except that I went straight for the pointer to function...

BUT, I can't figure out how the heck to create a pointer to member function INSIDE the same class. I get a "term does not evaluate to a function".

I define this in the header
void BlitSample::testfunction(void);
void (BlitSample::*FP)();

And use
FP = testfunction;
to set the pointer and call it with
FP();

I can get it to work if I leave it outside the class definition, but I'd like to keep it in the class if possible. Of course, if I did get it to work, I'd change it to an array of function pointers and use it instead of my switch() statement...

EDIT!
I now see that I need to call the function with
(this->*FP)();

or have a static function whose pointer would then just be the standard "void (*FP)()"
eric,

Have you profiled the code without the switch and compared it to the code you've posted above for each of the cases? No matter what method you use (templates, function pointers, self-modifying code, etc.) it will never be faster than the code without the switch for executing a single case.

If the difference between having the switch there and not for a single case is so miniscule that you'll not notice a signifcant increase in the overall performance of your app, don't bother with this and try to find other areas that offer a greater return for your effort.

One of these days I'm going to write a whitepaper on how to properly attack the problem of code optimization on Windows CE platforms, but lesson one would certainly be to measure your code to find out where the bottlenecks are rather than making assumptions.
Hi all,

Thanks for all the replies. I tend to always forget the fact that templates are primarily compile-time tools.

Dan: I'll be using function pointers, thanks.

Digby: you are right. I just saw a chance to get rid of a dummy switch() and I couldn't resist! Thanks for the tips, and get on that optimization article! ;)

--Eric