Advertisement
makiolo

using custom-apply on templated function

Aug 29th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <tuple>
  3. #include <map>
  4. #include <functional>
  5.  
  6. template <class F, size_t... Is>
  7. constexpr auto index_apply_impl(F&& f, std::index_sequence<Is...>) {
  8.     return f(std::integral_constant<size_t, Is> {}...);
  9. }
  10.  
  11. template <size_t N, class F>
  12. constexpr auto index_apply(F&& f) {
  13.     return index_apply_impl(std::forward<F>(f), std::make_index_sequence<N>{});
  14. }
  15.  
  16. template <typename ... Args>
  17. constexpr void apply(const std::function<void(Args...)>& f, const std::tuple<Args...>& t)
  18. {
  19.     index_apply<std::tuple_size<std::tuple<Args...> >{}>(
  20.             [&](auto... Is) {
  21.                 f(std::get<Is>(t)...);
  22.             });
  23. }
  24.  
  25. void printme(int param)
  26. {
  27.     std::cout << param << std::endl;
  28. }
  29.  
  30. template <typename ... Args>
  31. void printme(int param, Args... data)
  32. {
  33.     std::cout << param << std::endl;
  34.     printme(std::forward<Args>(data)...);
  35. }
  36.  
  37. int main()
  38. {
  39.     using namespace std::placeholders;
  40.     printme(1, 2, 3, 4, 5);
  41.     //auto printme_binded = std::bind(printme, _1, _2, _3, _4, _5);
  42.     //printme_binded(1, 2, 3, 4, 5);
  43.     apply(printme, std::make_tuple(1, 2, 3, 4, 5));
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement