Assume I have a tuple, e.g. using BaseTuple = std::tuple<int, double, string>;
, I'd like to define new types from a given variadic index sequence, and methods to convert to and from BaseTuple. For example, CustomTuple<0,1> should derive from tuple<int, double>.
using BaseTuple = tuple<int, double, string>;
template <size_t... I>
struct CustomTuple: public tuple<tuple_element_t<I, BaseTuple>...>{
using tuple<tuple_element_t<I, BaseTuple>...>::tuple;
static CustomTuple fromBaseTuple(const BaseTuple& key){
return {get<I>(key)...};
};
BaseTuple toBaseTuple(){
BaseTuple t;
((get<tuple_element_t<I, BaseTuple>>(t) = get<tuple_element_t<I, BaseTuple>>(t)), ...);
return t;
}
};
The above code works if tuple has unique types, but I wonder if there's any way for duplicate types. I can't find a way to iterate I... with its index.
Something along these lines:
template <std::size_t... S>
BaseTuple toBaseTuple(std::index_sequence<S...>) {
BaseTuple t;
((std::get<I>(t) = std::get<S>(*this)), ...);
return t;
}
BaseTuple toBaseTuple(){
return toBaseTuple(std::make_index_sequence<sizeof...(I)>{});
}