Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tuple>
- #include <iostream>
- #include <vector>
- #include <list>
- template<int Count, class T, class InputIterator>
- struct MakeTuple
- {
- static auto fromCollection(InputIterator first, InputIterator last) -> decltype(std::tuple_cat(MakeTuple<Count - 1, T, InputIterator>::fromCollection(first, last), std::make_tuple(*--last)))
- {
- return std::tuple_cat(MakeTuple<Count - 1, T, InputIterator>::fromCollection(first, last), std::make_tuple(*--last));
- }
- };
- template<class T, class InputIterator>
- struct MakeTuple<1, T, InputIterator>
- {
- static std::tuple<T> fromCollection(InputIterator first, InputIterator last)
- {
- return std::make_tuple(*first);
- }
- };
- //
- template<class T, size_t Size>
- class StaticDimentionList
- {
- public:
- friend class StaticDimentionList<T, Size + 1>;
- enum {
- static_size = Size
- };
- StaticDimentionList(const StaticDimentionList<T, Size - 1>& other)
- :
- m_list(other.m_list)
- {}
- StaticDimentionList<T, Size + 1> push_back(const T& item)
- {
- m_list->push_back(item);
- return StaticDimentionList<T, Size + 1>(*this);
- }
- inline const std::list<T>& get() { return *m_list; }
- private:
- std::list<T>* m_list;
- };
- template<class T>
- class StaticDimentionList<T, 0>
- {
- public:
- friend class StaticDimentionList<T, 1>;
- StaticDimentionList()
- {
- m_list = new std::list<T>();
- }
- ~StaticDimentionList()
- {
- delete m_list;
- }
- enum {
- static_size = 0
- };
- StaticDimentionList<T, 1> push_back(const T& item)
- {
- m_list->push_back(item);
- return StaticDimentionList<T, 1>(*this);
- }
- inline const std::list<T>& get() { return *m_list; }
- private:
- std::list<T>* m_list;
- };
- int main()
- {
- typedef StaticDimentionList<int, 0> list;
- list lst;
- auto res = lst.push_back(1).push_back(2).push_back(3);
- typedef decltype(res) res_t;
- auto tup = MakeTuple<res_t::static_size, int, decltype(lst.get().begin())>::fromCollection(lst.get().begin(), lst.get().end());
- std::cout << std::get<0>(tup) << std::get<1>(tup) << std::get<2>(tup);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement