SHARE
TWEET

Untitled

a guest Mar 26th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <tuple>
  2. #include <utility>
  3. #include <iostream>
  4.  
  5. class Aa {
  6.     public:
  7.         Aa(int a1_, int a2_): a1(a1_), a2(a2_) {}
  8.  
  9.         template<std::size_t N>
  10.         decltype(auto) get() const {
  11.             if constexpr (N == 0) return a1;
  12.             else if constexpr (N == 1) return a2;
  13.         }
  14.  
  15.     private:
  16.         int a1;
  17.         int a2;
  18. };
  19.  
  20. class Bb {
  21.     public:
  22.         Bb(Aa a_, int b_): a(a_), b(b_) {}
  23.  
  24.         template<std::size_t N>
  25.         decltype(auto) get() const {
  26.             if constexpr (N == 0) return a;
  27.             else if constexpr (N == 1) return b;
  28.         }
  29.  
  30.     private:
  31.         Aa a;
  32.         int b;
  33. };
  34.  
  35.  
  36. namespace std {
  37.     // Aa
  38.     template<>
  39.     struct tuple_size<Aa> : std::integral_constant<std::size_t, 2> {};
  40.  
  41.     template<std::size_t N>
  42.     struct tuple_element<N, Aa> {
  43.         using type = decltype(std::declval<Aa>().get<N>());
  44.     };
  45.  
  46.     // Bb
  47.     template<>
  48.     struct tuple_size<Bb> : std::integral_constant<std::size_t, 2> {};
  49.  
  50.     template<std::size_t N>
  51.     struct tuple_element<N, Bb> {
  52.         using type = decltype(std::declval<Bb>().get<N>());
  53.     };
  54. }
  55.  
  56.  
  57. template
  58. <
  59.   typename T,
  60.   typename tp_enabled =
  61.     std::void_t
  62.     <
  63.       decltype(std::tuple_size<T>::value),
  64.       decltype(std::declval<T&>().template get<std::size_t(0)>())
  65.     >
  66. >
  67. constexpr auto
  68. is_tuple_like(int const)noexcept->bool
  69. {
  70.   return true;
  71. }
  72.  
  73. template
  74. <typename T, typename tp_arg>
  75. constexpr auto
  76. is_tuple_like(tp_arg const)noexcept->bool
  77. {
  78.   return false;
  79. }
  80.  
  81. template<typename T>
  82. auto encode(T const& t, std::ostream& os)->
  83. std::enable_if_t<is_tuple_like<T>(0)>
  84. {
  85.   encode_impl(t, os, std::make_index_sequence<std::tuple_size<T>::value>{ });
  86.   os << std::endl;
  87. }
  88.  
  89. template
  90. <bool is_last, typename T>
  91. auto
  92. encode_one(T const& t, std::ostream& os)->
  93. std::enable_if_t<!is_tuple_like<T>(0)>
  94. {
  95.   os << t << (is_last ? " " : ", ");
  96. }
  97.  
  98. template
  99. <bool is_last, typename T>
  100. auto
  101. encode_one(T const& t, std::ostream& os)->
  102. std::enable_if_t<is_tuple_like<T>(0)>
  103. {
  104.   encode_impl(t, os, std::make_index_sequence<std::tuple_size<T>::value>{ });
  105.   os << (is_last ? " " : ", ");
  106. }
  107.  
  108. template
  109. <typename T, std::size_t... I>
  110. void encode_impl(T const& t, std::ostream& os, std::index_sequence<I...> const)
  111. {
  112.   constexpr auto last = sizeof...(I) - 1;
  113.   os << "{ ";
  114.   [[maybe_unused]] int const temp[] =
  115.     { (encode_one<I == last>(t.template get<I>(), os), 0)... };
  116.   os << "}";
  117. }
  118.  
  119. int main () {
  120.     auto a = Aa(1, 1);
  121.     encode(a, std::cout);
  122.     auto b = Bb(a, 1);
  123.     encode(b, std::cout);
  124.     return 0;
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top