Last active
March 3, 2017 07:28
-
-
Save Starl1ght/376d842837aba40c2561bf7741a8a388 to your computer and use it in GitHub Desktop.
Lesson
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 <iostream> | |
#include <tuple> | |
#include <vector> | |
// ========== VARIADIC | |
void PrintInternal() { | |
std::cout << std::endl; | |
} | |
// it needs to be visible | |
template <typename T, typename...ARGS> | |
void PrintInternal(const T& head, const ARGS&...rest); | |
template <typename...ARGS> | |
void PrintInternal(const double& head, const ARGS&...rest) { // #3 | |
std::cout << "DBL!!! " << head << " "; | |
PrintInternal(rest...); | |
} | |
template <typename T, typename...ARGS> | |
void PrintInternal(const T& head, const ARGS&...rest) { | |
std::cout << head << " "; | |
PrintInternal(rest...); | |
}; | |
template <typename...ARGS> | |
void Print(const ARGS&...args) { | |
PrintInternal(args...); | |
} | |
// ============ VECTOR & UNPACK | |
template <typename...ARGS> | |
std::vector<int> MakeVector(ARGS...args) { | |
return std::vector<int> { args... }; | |
} | |
// ============ MakeTuple | |
template <typename...ARGS> | |
std::tuple<ARGS...> MakeTuple(ARGS...args) { | |
return std::tuple<ARGS...>(args...); | |
} | |
// =========== FOR EACH TUPLE | |
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 ForEachTpl(std::tuple<TYPES...>& tpl, CALLABLE&& func) { | |
constexpr size_t tplSize = sizeof...(TYPES); | |
ForEachIter_t<0, tplSize>::Do(tpl, func); | |
} | |
// ============ Distinguish elements | |
template <typename ELEM, typename ALLOC> | |
void Info(std::vector<ELEM, ALLOC> vec) {}; | |
template <template <typename, typename> class T, typename U, typename I> | |
void Nested(T<U, I> container) {}; | |
// ============ Saving types | |
template <typename T> | |
struct AddPtr { | |
using type = T*; | |
}; | |
template <typename T> | |
using AddPtr_t = typename AddPtr<T>::type; | |
// ============= Saving values | |
template <size_t A, size_t B> | |
struct Sum { | |
static constexpr size_t result = A + B; | |
}; | |
int main() { | |
Print(1.1, 2, 3.3f, "string"); | |
const auto vec = MakeVector(1,2,3,4,5); | |
auto tpl1 = MakeTuple(1, 4.4, 3u, 2.2f); | |
ForEachTpl(tpl1, [](auto& elem) { | |
std::cout << elem << ' '; | |
}); | |
std::cout << std::endl; | |
Info(std::vector<int>{}); | |
Nested(std::vector<int>{}); | |
typename AddPtr<int>::type ptr = nullptr; | |
AddPtr_t<int> ptr2 = nullptr; | |
auto Four = Sum<1,3>::result; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment