压缩`std :: tuple`和可变参数

Bar*_*uch 11 c++ variadic-templates c++11 c++14

我有以下课程:

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

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

我将如何实现foo这样它分配给每个std::mapmaps得到与它匹配的钥匙叫什么名字?

例如,如果我有

C<int, int, float, std::string> c;
Run Code Online (Sandbox Code Playgroud)

我打来电话

c.foo(1, 2, 3.3, "qwerty")
Run Code Online (Sandbox Code Playgroud)

c.maps应该相当于

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);
Run Code Online (Sandbox Code Playgroud)

Pio*_*cki 8

#include <unordered_map>
#include <utility>
#include <tuple>
#include <cstddef>

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

    template <typename... Args>
    void foo(Args&&... keys)
    {
        foo_impl(std::make_index_sequence<sizeof...(Args)>{}, std::forward<Args>(keys)...);
    }

private:
    template <typename... Args, std::size_t... Is>
    void foo_impl(std::index_sequence<Is...>, Args&&... keys)
    {
        using expand = int[];
        static_cast<void>(expand{ 0, (
            std::get<Is>(maps)[std::forward<Args>(keys)] = 1
        , void(), 0)... });
    }
};
Run Code Online (Sandbox Code Playgroud)

DEMO