Created
February 1, 2017 16:15
-
-
Save Starl1ght/e301dacde747623193ae12d8052e1c29 to your computer and use it in GitHub Desktop.
toople
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tuple> | |
#include <iostream> | |
template <size_t CUR, size_t END> | |
struct ForEachIter_t { | |
template <typename CALLABLE, typename...TYPES> | |
static void Do(std::tuple<TYPES...>& tpl, CALLABLE&& func) { | |
auto& tplItem = std::get<CUR>(tpl); | |
func(tplItem); | |
ForEachIter_t<CUR + 1, END>::Do(tpl, func); | |
}; | |
}; | |
template <size_t CUR> | |
struct ForEachIter_t<CUR, CUR> { | |
template <typename CALLABLE, typename...TYPES> | |
static void Do(std::tuple<TYPES...>&, CALLABLE&&) {}; | |
}; | |
template <typename CALLABLE, typename...TYPES> | |
void ForEach(std::tuple<TYPES...>& tpl, CALLABLE&& func) { | |
constexpr size_t tplSize = std::tuple_size<std::tuple<TYPES...>>::value; | |
ForEachIter_t<0, tplSize>::Do(tpl, func); | |
} | |
template<size_t DST_BEG, size_t CUR, size_t END> | |
struct SumIter_t { | |
template<typename...T1, typename...T2> | |
static void Do(const std::tuple<T2...>& src, std::tuple<T1...>& dst) { | |
std::get<DST_BEG + CUR>(dst) = std::get<CUR>(src); | |
SumIter_t<DST_BEG, CUR + 1, END>::Do(src, dst); | |
} | |
}; | |
template<size_t DST_BEG, size_t CUR> | |
struct SumIter_t<DST_BEG, CUR, CUR> { | |
template<typename...T1, typename...T2> | |
static void Do(const std::tuple<T2...>& src, std::tuple<T1...>& dst) {} | |
}; | |
template <typename...T1, typename...T2> | |
std::tuple<T1..., T2...> ConcatTuple(const std::tuple<T1...>& tpl1, const std::tuple<T2...>& tpl2) { | |
std::tuple<T1..., T2...> ret; | |
SumIter_t<0, 0, sizeof...(T1)>::Do(tpl1, ret); | |
SumIter_t<sizeof...(T1), 0, sizeof...(T2)>::Do(tpl2, ret); | |
return ret; | |
} | |
void main() { | |
auto tpl1 = std::make_tuple(1, 4.4, 3u, 2.2f); | |
auto tpl2 = std::make_tuple(4, 3.3f, 2u, 1.1); | |
auto ret = ConcatTuple(tpl1, tpl2); | |
ForEach(ret, [] (const auto& itm) { | |
std::cout << itm << std::endl;; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment