c++ 中的模板方法模式和长参数列表
在对我的最后一个问题提供有用的答案后,我开始使用模板方法模式一堂有很多不同选择的课程。在没有全部实现的情况下,我当前对该类的对象的声明现在看起来像这样:
pc < prg, tc, 9, 0, 4, 4, test, true, true, true, true, false, true, true, 10, 0, -1, 3, 3 > mp;
如何处理长模板参数列表?我应该使用枚举/定义而不是真/假和数字吗?有常用的替代品吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我有一个那么长的模板参数列表,我宁愿重新考虑我的设计。许多参数通常表明设计不良。
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.
是的,使用枚举(而不是定义)而不是 true/false。这样,如果你获取的参数顺序不正确,编译器就会抱怨。而且,对于读者来说也更加清晰。
至于一般处理长参数列表——将它们隐藏在 typedef 或修复某些模板参数的生成器后面,并允许您改变其他参数。
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.
我不做 C++,这可能不太适用于模板,但我会尝试使用具有长参数列表的普通方法来查找相关参数并尝试对它们进行合理的分组并引入反映分组的参数对象,这样您就可以得到更短的更复杂的参数列表。
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.
我会评估传递一个参数,它是变体类型的集合,例如 std::vector。
I would rater pass a single argument which is a collection of a variant type, for example a std::vector.