I need a function that will behave like std::transform for tuples. Basically the functionality that will be implemented is
template<size_t From, size_t To, class Tuple, class Func> void tuple_transform(Tuple&& source, Tuple&& target, Func f) {
I believe that to implement this I will need to compile the integer range struct, generalize std::index_sequence , and I implemented it here with cti::range . I also believe that this type of compile-time bypass is ideal here:
template<class Func, class Tuple, size_t...Is> void for_each_in_tuple(Func f, Tuple&& tuple, std::index_sequence<Is...>){ using expander = int[]; (void)expander { 0, ((void)f(std::get<Is>(std::forward<Tuple>(tuple))), 0)... }; } template<class Func, class Tuple> void for_each_in_tuple(Func f, Tuple&& tuple){ for_each_in_tuple(f, std::forward<Tuple>(tuple), std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>()); }
Can someone help me with the implementation?
Notes. About the type of mutation function
@MohitJain Like the related code (tuple_transform), this is not taken into account ( Func has one type). If this is resolved this way, I could easily expand it by passing the template parameter template<class> class Func and indicate that my conversion types are similar to this
template<typename T> struct Func { static void apply(T& val) { ... } }
then inside the body of the tuple transform, each func transform can be called as:
get<I>(target) = func<typename tuple_element<I, Tuple>::type>::apply(get<I>(source))
EDIT
There was just a lightning current @accu 2015. The above was CodeKata at the end of the presentation. I will leave a presentation here , I hope that this will help with any attempts at implementation (I think that almost every necessary tool is required, so we will have more attempts)
c ++ tuples template-meta-programming variadic-templates
Nikos Athanasiou
source share