避免带默认参数的类模板的支架 [英] Avoiding Brackets for Class Template Having Default Parameter
问题描述
我有一个类似于下面的类模板,该模板旨在包含解析CSV文件时使用的一些配置设置:
I have a class template similar to the one that follows below that is designed to contain some configuration settings used when parsing CSV files:
template <typename InputIterator = default_all>
class icsv_params
{
// Iterator to a data structure containing the columns
// that should be read.
typedef InputIterator iterator;
// This is a bitmask type.
typedef detail::icsv_op icsv_op;
static const icsv_op noqt = icsv_op(detail::csv_flags::noqt);
static const icsv_op quot = icsv_op(detail::csv_flags::quot);
static const icsv_op mmap = icsv_op(detail::csv_flags::mmap);
// The rest of the class definition isn't relevant.
};
现在,当用户希望向数据结构提供开始和结束迭代器时,模板参数很重要包含应解析的列数;然而,如果用户无法将迭代器提供为参数,则类应自动假定应对所有列进行解析。
Now, the template parameter is important when the user wishes to supply start and end iterators to a data structure containing the numbers of the columns that should be parsed; however, should the user fail to provide the iterators as parameters, the class should automatically assume that all of the columns should be parsed.
在第二种情况下,代码声明一个类的实例看起来很笨重:
In the second case, the code to declare an instance of the class looks unwieldy:
icsv_params<> params(...);
此外,位掩码类型 noqt
,
和 mmap
仅由此类使用,因此将它们放在类定义中是有意义的;然而,如果用户希望使用这些位掩码类型,那么这样做的代码也是笨拙的:
Additionally, the bitmask types noqt
, quot
, and mmap
are used by this class only, so it makes sense to put them inside the class definition; however, should the user wish to use these bitmask types, the code to do so is also unwieldy:
icsv_params<> params(icsv_params<>::noqt);
如何使用它,以便用户不需要提供尖括号来表示缺席的模板参数?如果没有办法这样做,你会建议什么替代方案?
How can I make it so that the user does not need to provide the angle brackets to indicate the absence of a template parameter? If there is no way to do so, what alternative would you suggest?
推荐答案
不幸的是,这是C ++语法。 IIRC,在C ++ 0x中,有关联的命名空间(解决你的第二个问题)。
Unfortunately this is C++ syntax. IIRC, in C++0x, there are associated namespaces (which solve your second question).
对于第一个,typedef应该做,àla STL:
For the first one, a typedef should do, à la STL:
template <typename InputIterator = default_all>
class basic_icsv_params
{
...
};
typedef basic_icsv_params<> icsv_params:
这篇关于避免带默认参数的类模板的支架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!