fork(2) download
  1. #include <tuple>
  2. #include <utility>
  3.  
  4. #include<boost/fusion/adapted/std_tuple.hpp>
  5. #include <boost/fusion/algorithm/transformation/reverse.hpp>
  6. #include <boost/fusion/include/reverse.hpp>
  7.  
  8. #include <boost/fusion/sequence/intrinsic/size.hpp>
  9. #include <boost/fusion/include/size.hpp>
  10.  
  11. #include <iostream>
  12.  
  13. template <std::size_t... Is>
  14. struct indices {};
  15.  
  16. template <std::size_t N, std::size_t... Is>
  17. struct build_indices
  18. : build_indices<N-1, N-1, Is...> {};
  19.  
  20. template <std::size_t... Is>
  21. struct build_indices<0, Is...> : indices<Is...> {};
  22.  
  23. template<typename Sequence, std::size_t ...Is>
  24. auto as_std_tuple_impl(const Sequence& s, indices<Is...>&&) -> decltype(std::tie(boost::fusion::at_c<Is>(s)...))
  25. {
  26. return std::tie(boost::fusion::at_c<Is>(s)...);
  27. }
  28.  
  29. template <typename Sequence, typename Indices = build_indices<boost::fusion::result_of::size<Sequence>::value>>
  30. auto as_std_tuple(const Sequence& s)
  31. {
  32. return as_std_tuple_impl(s, Indices());
  33. }
  34.  
  35.  
  36. template<class Tuple, std::size_t N>
  37. struct TuplePrinter
  38. {
  39. static void print(const Tuple& t)
  40. {
  41. TuplePrinter<Tuple, N-1>::print(t);
  42. std::cout << ", " << std::get<N-1>(t);
  43. }
  44. };
  45.  
  46. template<class Tuple>
  47. struct TuplePrinter<Tuple, 1>
  48. {
  49. static void print(const Tuple& t)
  50. {
  51. std::cout << std::get<0>(t);
  52. }
  53. };
  54.  
  55. template<class... Args>
  56. void print(const std::tuple<Args...>& t)
  57. {
  58. std::cout << "(";
  59. TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
  60. std::cout << ")\n";
  61. }
  62.  
  63. int main()
  64. {
  65. std::tuple<int, double, std::string> tup(1,2.5,"hello");
  66. auto view_rev = boost::fusion::reverse(tup);
  67. auto reversed_tup = as_std_tuple(view_rev);
  68.  
  69. print(tup);
  70. print(reversed_tup);
  71. return 0;
  72. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
(1, 2.5, hello)
(hello, 2.5, 1)