mercredi 29 juillet 2020

C++ typedef with tuple of indefinite size

I am working with some C++ code that creates a set from quaternary tuples.

I would like to generalise the code so that tuples of any magnitude could be added to the set.

Is there a way of creating a typedef for a tuple of an indefinite number of elements?

For the sake of compatibility with the project that I working on I would like this to be implemented with C++11 features. I am hoping to keep typedefs if possible so that it is consistent with the rest of the programming.

I have looked at a lot of information on variadic functions and templates, but I am still unsure how to proceed.

#include <set>
#include <functional>
#include <iostream>



typedef std::set<std::tuple<int, int, int, int>> QuaternarySet;
typedef std::tuple<int, int, int, int> QuaternaryTuple;

int main () {

    QuaternaryTuple x = std::make_tuple(1, 2, 3, 4);
    QuaternaryTuple y = std::make_tuple(5, 6, 7, 8);
    QuaternaryTuple z = std::make_tuple(1, 2, 3, 4);

    QuaternarySet s;

    s.insert(x);
    s.insert(y);
    s.insert(z);

    std::cout << s.size() << std::endl;
}

Ideally, I am looking for some code that allows me to define something like this but I am not sure what to replace the ellipses with.

typedef std::set<std::tuple<int ...>> IndefiniteSet;
typedef std::tuple<int ...> IndefiniteTuple;

That is to say, I need a run time variable size container of a single type, int.

Aucun commentaire:

Enregistrer un commentaire