Advertisement
Guest User

...

a guest
Oct 13th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <tuple>
  2. #include <iostream>
  3. #include <vector>
  4. #include <list>
  5.  
  6. template<int Count, class T, class InputIterator>
  7. struct MakeTuple
  8. {
  9.     static auto fromCollection(InputIterator first, InputIterator last) -> decltype(std::tuple_cat(MakeTuple<Count - 1, T, InputIterator>::fromCollection(first, last), std::make_tuple(*--last)))
  10.     {
  11.         return std::tuple_cat(MakeTuple<Count - 1, T, InputIterator>::fromCollection(first, last), std::make_tuple(*--last));
  12.     }
  13. };
  14.  
  15. template<class T, class InputIterator>
  16. struct MakeTuple<1, T, InputIterator>
  17. {
  18.     static std::tuple<T> fromCollection(InputIterator first, InputIterator last)
  19.     {
  20.         return std::make_tuple(*first);
  21.     }
  22. };
  23.  
  24. //
  25.  
  26. template<class T, size_t Size>
  27. class StaticDimentionList
  28. {
  29. public:
  30.     friend class StaticDimentionList<T, Size + 1>;
  31.  
  32.     enum {
  33.         static_size = Size
  34.     };
  35.  
  36.     StaticDimentionList(const StaticDimentionList<T, Size - 1>& other)
  37.         :
  38.         m_list(other.m_list)
  39.     {}
  40.  
  41.     StaticDimentionList<T, Size + 1> push_back(const T& item)
  42.     {
  43.         m_list->push_back(item);
  44.         return StaticDimentionList<T, Size + 1>(*this);
  45.     }
  46.  
  47.     inline const std::list<T>& get() { return *m_list; }
  48.  
  49. private:
  50.     std::list<T>* m_list;
  51. };
  52.  
  53. template<class T>
  54. class StaticDimentionList<T, 0>
  55. {
  56. public:
  57.     friend class StaticDimentionList<T, 1>;
  58.  
  59.     StaticDimentionList()
  60.     {
  61.         m_list = new std::list<T>();
  62.     }
  63.  
  64.     ~StaticDimentionList()
  65.     {
  66.         delete m_list;
  67.     }
  68.  
  69.     enum {
  70.         static_size = 0
  71.     };
  72.  
  73.     StaticDimentionList<T, 1> push_back(const T& item)
  74.     {
  75.         m_list->push_back(item);
  76.         return StaticDimentionList<T, 1>(*this);
  77.     }
  78.  
  79.     inline const std::list<T>& get() { return *m_list; }
  80.  
  81. private:
  82.     std::list<T>* m_list;
  83. };
  84.  
  85. int main()
  86. {  
  87.     typedef StaticDimentionList<int, 0> list;
  88.     list lst;
  89.    
  90.     auto res = lst.push_back(1).push_back(2).push_back(3);
  91.     typedef decltype(res) res_t;
  92.  
  93.     auto tup = MakeTuple<res_t::static_size, int, decltype(lst.get().begin())>::fromCollection(lst.get().begin(), lst.get().end());
  94.     std::cout << std::get<0>(tup) << std::get<1>(tup) << std::get<2>(tup);
  95.     getchar();
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement