lundi 23 mai 2016

call a function on each pair of tuple values

I have the following class:

template<typename... Tkeys>
class C
{
public:
    std::tuple<std::unordered_map<Tkeys, int>... > maps;

    // Not real function:
    void foo(Tkeys... keys) {
        maps[keys] = 1;
    }
};

How would I implement foo so that it assigns to each std::map in maps gets called with it matching key?

For example, if I have

C<int, int, float, std::string> c;

and I called

c.foo(1, 2, 3.3, "qwerty")

then c.maps should be equivalent to

m1 = std::map<int, int>()
m1[1] = 1;
m2 = std::map<int, int>()
m2[2] = 1;
m3 = std::map<float, int>()
m3[3.3] = 1;
m4 = std::map<std::string, int>()
m4["qwerty"] = 1;
c.maps = std::make_tuple(m1, m2, m3, m4);

Aucun commentaire:

Enregistrer un commentaire