Zipping an `std::tuple` and variadic arguments
#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)... });
}
};
DEMO
If you have a compiler that supports C++17 fold expressions then you could have the following simple variadic expansion scheme:
template<typename... Tkeys>
class C {
template<typename... Args, std::size_t... I>
void foo_helper(std::index_sequence<I...>, Args&& ...args) {
((std::get<I>(maps)[std::forward<Args>(args)] = 1), ...);
}
public:
std::tuple<std::unordered_map<Tkeys, int>... > maps;
void foo(Tkeys... keys) {
foo_helper(std::index_sequence_for<Tkeys...>{}, std::forward<Tkeys>(keys)...);
}
};
Live Demo