template method pattern and long parameter lists in c++
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/defin开发者_开发百科es instead of true/false and numbers? Are there commonly used alternatives?
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.
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.
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.
I would rater pass a single argument which is a collection of a variant type, for example a std::vector.
精彩评论