模板方法模式和长参数列表 [英] template method pattern and long parameter lists in c++

查看:113
本文介绍了模板方法模式和长参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对我的最后一个问题的帮助答复后,我开始使用模板方法模式为一个类有很多不同的选项。如果没有实现它们,我目前的声明对象的类现在看起来像这样:

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?

推荐答案

是,使用枚举(而不是定义)而不是true / false。这样,如果你得到的参数乱序,那么编译器会抱怨。

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.

对于处理长参数列表,一般来说,将它们隐藏在一个typedef后面,或者是一个生成器,它修复了一些模板参数,并允许您更改其他。

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.

这篇关于模板方法模式和长参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆