Many template function arguments

Started by
4 comments, last by cache_hit 14 years, 1 month ago
Hello everyone. I have a rather simple problem and I thought maybe there is something I'm missing that would help me with it. I have a function that loops over a number of objects and doe some calculations and modifies them, and I'd like to keep that function fast, so I made it a template function with template bool parameters that control whether certain calculations are performed or not. Here:

template <bool bPreserveCorners, bool bSmoothPositions, bool bSmoothThickness, bool bSmoothMasking>
VOID SmoothCurve( CSmoothTool* pSmoothTool, CCurve* pCurve, INT nKernelSize, TCurveNode* pFirst, TCurveNode* pLast )

The problem is the complexity of the code that determines the template arguments to pass to this function. How can I pass the boolean variables as template arguments without creating a complex if tree? Truth be told, I will actually just use only one template argument (bPreserveCorners in this case) since using four would result in 16 versions of the function being instantiated and linked, and since the performance gains from templating the other 3 boolean arguments is in practice insubstantial and unnecessary, but I still would like to know if there is an easy way to approach this. Thanks in advance.
Advertisement
Use a function parameter, not a template parameter. Either you're going to have to put an if-statement at the call-site to determine which version to call, or you're going to have to put an if-statement inside the function to determine what to do, so templated or not is not going to have much effect. Besides, that's a pretty serious micro-optimization -- make it work first, profile later.
You can use policies to accomplish the same thing in a more readable and parametric manner. Personally, I think C++ policy-based design is hideous, but many others disagree with me.
Quote:Original post by Sneftel
You can use policies to accomplish the same thing in a more readable and parametric manner. Personally, I think C++ policy-based design is hideous, but many others disagree with me.


Eh, most of C++ having to do with templates and metaprogramming is hideous if you're trying to read it. If you're just trying to use it though it's not too bad. Unless of course the library was designed by someone with an IQ of less than 200, then it can be hideous.

I wouldn't say the *idea* of using policies is necessarily bad, after all allocators are just an example of policies. But like anything, it's kind of easy to go overboard if you're not careful.
This policy business needs at least some getting used to in order for one to be able to efficiently use it without cursing at the monitor, it seems. But since I just can't help but optimize things as much as I can when the situation allows it, I'll definitely look into it later.

Quote:Use a function parameter, not a template parameter. Either you're going to have to put an if-statement at the call-site to determine which version to call, or you're going to have to put an if-statement inside the function to determine what to do, so templated or not is not going to have much effect. Besides, that's a pretty serious micro-optimization -- make it work first, profile later.


I'm Already doing that, I just wondered if there was some "magic" way to sort this out if one wanted to use many template parameters without lots of if statements at the call site. Unsurprisingly, there doesn't seem to be. Oh well. Thanks for the input. Expect a curvy Image of the Day entry soon :D
Quote:Original post by Amr0
This policy business needs at least some getting used to in order for one to be able to efficiently use it without cursing at the monitor, it seems. But since I just can't help but optimize things as much as I can when the situation allows it, I'll definitely look into it later.

Quote:Use a function parameter, not a template parameter. Either you're going to have to put an if-statement at the call-site to determine which version to call, or you're going to have to put an if-statement inside the function to determine what to do, so templated or not is not going to have much effect. Besides, that's a pretty serious micro-optimization -- make it work first, profile later.


I'm Already doing that, I just wondered if there was some "magic" way to sort this out if one wanted to use many template parameters without lots of if statements at the call site. Unsurprisingly, there doesn't seem to be. Oh well. Thanks for the input. Expect a curvy Image of the Day entry soon :D


Yea there really isn't. Templates should be used when you always know values at compile time. If the way you parameterize a function can only be known at runtime you're you're asking for trouble making it template-based.

This topic is closed to new replies.

Advertisement