Reputation: 163
The methods in this class use an index sequence containing a sequence of indices covering the entire data
tuple (an index for each argument passed in the parameter pack). Per other answers I've seen, I'm currently templating my methods as such:
#include <tuple>
#include <memory>
#include <vector>
using std::tuple;
template <typename... Args>
class Foo
{
protected:
tuple<std::shared_ptr<std::vector<Args>>...> data;
private:
template<size_t...Is>
void _clear_(std::index_sequence<Is...>) {
(std::get<Is>(data)->clear(), ...);
};
public:
void clear()
{ _clear_(std::index_sequence_for<Args...>()); };
};
This seems messy, though. I was wondering if there's a way that I could define Is
at a class-level scope (or, more generally, whether there's a better way to do this).
Upvotes: 2
Views: 57
Reputation: 217085
With C++17, you might simply use std::apply
:
void clear()
{
std::apply([](auto&... vs){ (vs.clear(), ...); }, data);
}
I was wondering if there's a way that I could define
Is
at a class-level scope
With extra layer:
template <typename Seq, typename...> class FooImpl;
template <std::size_t... Is, typename... Ts>
class FooImpl<std::index_sequence<Is...>, Ts...>
{
protected:
std::tuple<std::shared_ptr<std::vector<Ts>>...> data;
public:
void clear() { (std::get<Is>(data).clear(), ...); };
};
template <typename... Ts>
using Foo = FooImpl<std::make_index_sequence<sizeof...(Ts)>, Ts...>;
Upvotes: 8