Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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; 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?

like image 802
void-pointer Avatar asked Jul 25 '11 06:07

void-pointer


People also ask

Can template parameters have default values?

Like function default arguments, templates can also have default arguments. For example, in the following program, the second parameter U has the default value as char.

Can default argument be used with the template class?

Can default arguments be used with the template class? Explanation: The template class can use default arguments.

Can we pass Nontype parameters to templates?

Template classes and functions can make use of another kind of template parameter known as a non-type parameter. A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument.

How will you restrict the template for a specific datatype?

There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.


1 Answers

Unfortunately this is C++ syntax. IIRC, in C++0x, there are associated namespaces (which solve your second question).

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:
like image 146
Alexandre C. Avatar answered Sep 28 '22 00:09

Alexandre C.