문제

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?

도움이 되었습니까?

해결책

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

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top