简体   繁体   English

避免具有默认参数的类模板的括号

[英]Avoiding Brackets for Class Template Having Default Parameter

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: 我有一个类模板,类似于下面的类模板,旨在包含解析CSV文件时使用的一些配置设置:

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(...);

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; 另外,这个类只使用bitmask类型noqtquotmmap ,所以将它们放在类定义中是有意义的。 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? 如果没有办法这样做,你会建议什么选择?

Unfortunately this is C++ syntax. 不幸的是,这是C ++语法。 IIRC, in C++0x, there are associated namespaces (which solve your second question). IIRC,在C ++ 0x中,有相关的命名空间(解决了你的第二个问题)。

For the first one, a typedef should do, à la STL: 对于第一个,一个typedef应该做的, 点菜 STL:

template <typename InputIterator = default_all>
class basic_icsv_params
{
    ...
};

typedef basic_icsv_params<> icsv_params:

Usually for iterator parameters, the iterator type is a template parameter to only the functions that need them. 通常对于迭代器参数,迭代器类型是仅对需要它们的函数的模板参数。 For example, if you look at the std::vector constructor, it's templated with begin() and end() iterators, but not the whole type. 例如,如果你看一下std::vector构造函数,它的模板是使用begin()和end()迭代器,而不是整个类型。

Putting angular braces in my opinion is a better way in fact. 事实上,在我看来放角度支撑是一种更好的方法。 Since, they cannot be ingonred, Alternative way can be, 既然,他们无法进行,替代方式可以,

typedef icsv_params<> icsv_params_default;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM