jeudi 9 mars 2017

How to write a variadic-template function with objects as parameters?

Consider the following function (it uses the CSV parser library from ben-strasser (github))

void col1(const std::string &fn, Base *v0)
{
    io::CSVReader<2> in(fn);
    in.read_header(io::ignore_extra_column, "epoch", v0->column);
    double ign;
    while (in.read_row(ign, v0->value)) {
        v0->process();
    }
}

This function processes the value in column 2 of a CSV-file. v0 of type Base * contains the member value which is filled by read_row and is processed in the process-method. Base is an interface-class of calculation methods (for exemple: one is Max, another one is MinMaxAvg).

How could I rewrite this function to accept any number of Base * arguments in order to process multiple columns?

read_header and read_row are variadic-template function and thus can accept any number of arguments, but they only work with scalars.

How do I expand/unpack the variadic-argument so that it calls or uses a member?

I tried some things, reading some examples, but I'm unable to create something which works, here is my current/ridicules code:

template<unsigned int COL>
void func(const std::string &fn, Base &... values)
{
     io::CSVReader<COL> in(fn);
     // that's it :-(
}

Aucun commentaire:

Enregistrer un commentaire