Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <initializer_list>
  2. #include <functional>
  3. #include <iostream>
  4. #include <utility>
  5. #include <tuple>
  6.  
  7. //--------------------------------------------------------
  8. // Appliquer une fonction sur une ligne d'arguments
  9. template<class F, class...Ts> F for_each_args(F f, Ts&&...a)
  10. {
  11. return (void)std::initializer_list<int>{((void)std::ref(f)(std::forward<Ts>(a)),0)...}, f;
  12. }
  13.  
  14. //--------------------------------------------------------
  15. // Appliquer une fonction sur chaque membres d'un tuple
  16. namespace detail
  17. {
  18. template <class F, class Tuple, std::size_t... I>
  19. decltype(auto) apply_impl( F&& f, Tuple&& t, std::index_sequence<I...> )
  20. {
  21. return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
  22. }
  23. }
  24.  
  25. template <class F, class Tuple> decltype(auto) apply(F&& f, Tuple&& t)
  26. {
  27. return detail::apply_impl(std::forward<F>(f), std::forward<Tuple>(t),
  28. std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>{}>{});
  29. }
  30.  
  31. //--------------------------------------------------------
  32. // Une reduction sans recursion
  33. namespace detail
  34. {
  35. template <class Array, std::size_t... I>
  36. decltype(auto) reduce_impl( Array const& a, std::index_sequence<I...> )
  37. {
  38. using type = typename Array::value_type;
  39. type r{};
  40.  
  41. (void)std::initializer_list<type>{ (r+= a[I])... };
  42.  
  43. return r;
  44. }
  45. }
  46.  
  47. template<class Array> decltype(auto) reduce(Array const& a)
  48. {
  49. return detail::reduce_impl(a, std::make_index_sequence<std::tuple_size<Array>{}>{});
  50. }
  51.  
  52. //--------------------------------------------------------
  53. template<bool... B> struct bools_ {}
  54. ;
  55. // Reduction de meta-predicat
  56. template<template<class> class Pred, typename... T>
  57. struct all : std::is_same< bools_<true , Pred<T>::value... >
  58. , bools_<Pred<T>::value..., true >
  59. >
  60. {};
  61.  
  62. //--------------------------------------------------------
  63. int main()
  64. {
  65. auto disp = [](auto x) { std::cout << x << " "; };
  66. for_each_args( disp, 1,"3",5.f,"rofl",39u );
  67. std::cout << "\n";
  68.  
  69. auto t = std::make_tuple(4,3.f,88u,'z');
  70. apply( [&](auto const&... x) { for_each_args( disp, x... ); } , t);
  71. std::cout << "\n";
  72.  
  73. std::array<float,5> ff{1,2,3,4,5};
  74. std::cout << reduce(ff) << "\n";
  75.  
  76. using is_all_pointer = all< std::is_pointer, char*, void**, float****>;
  77. std::cout << std::boolalpha << is_all_pointer::value << "\n";
  78.  
  79. using almost_all_pointer = all< std::is_pointer, char*, void**, long, float****>;
  80. std::cout << std::boolalpha << almost_all_pointer::value << "\n";
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement