Recursive pattern solution:
template <int n, int First, int ...Rest>
struct helper {
static void funcImpl(std::array<bool, SIZE>& temp) {
temp[First] = true;
helper<n - 1, Rest...>::funcImpl(temp);
}
};
template <int First>
struct helper<0, First> {
static void funcImpl(std::array<bool, SIZE>& temp) {
temp[First] = true;
}
};
template <int ...Args>
std::array<bool, SIZE> func() {
std::array<bool, SIZE> b = {};
helper<sizeof...(Args) - 1, Args...>::funcImpl(b);
return b;
}
EDIT: Super simplified version inspired by iavr solution:
template <int... A>
std::array<bool, SIZE> func() {
std::array<bool, SIZE> b = {};
auto values = {A...};
std::for_each(values.begin(), values.end(), [&](int n){b[n] = true;});
return b;
}
source
share