views:

111

answers:

4

After the helpful answers to my last question I started using the template method pattern for a class with a lot of different options. Without having implemented them all, my current declarations for objects of that class now look like this:

pc < prg, tc, 9, 0, 4, 4, test, true, true, true, true, false, true, true, 10, 0, -1, 3, 3 > mp;

How do you deal with long template parameter lists? Should I use enums/defines instead of true/false and numbers? Are there commonly used alternatives?

+1  A: 

I would rater pass a single argument which is a collection of a variant type, for example a std::vector.

martjno
I didn't do that, because I need the compiler optimizations for the template parameter constants instead of evaluating the elements of the collection at runtime.
Thomas
+4  A: 

Yes, use enums (not defines) instead of true/false. That way, if you get the parameters out of order, then the compiler will complain. Also, it's much clearer to readers.

As for dealing with with long parameter lists in general --- hide them behind a typedef, or a generator that fixes some of the template parameters, and lets you vary the others.

Anthony Williams
I guess by "generator" you mean something like `template < someparams > struct simple_pc { typedef pc < long list with some fixed values > T; };`? That sounds good.
Thomas
Yes, that's what I meant.
Anthony Williams
+2  A: 

I don't do c++, and this may not apply well to template, but what I would try to do with a normal method with a long parameter list is find related parameters and try to group them sensibly and introduce parameter objects reflecting the grouping, so that you have a shorter list of more complex parameters.

Don Roby
Sensible advice indeed :)
Matthieu M.
It took me a while to realize that this suggestion is applicable to my template scenario as well and leads to a good design: define some subtypes that take some of the parameters (according to those created by the template method pattern) and let the original class only take the subtypes as parameters and inherit from them.
Thomas
@Thomas: Glad to hear it was useful in this context. I'm going to have to brush up on my c++ and look into it someday.
Don Roby
+5  A: 

I would rather reconsider my design if I had a template parameter list which is THAT long. Many parameters are often an indication of bad design.

Laurens Ruijtenberg