Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <typename H, typename T>
- struct List{
- H head;
- T tail;
- typedef H head_t;
- typedef T tail_t;
- };
- template<>
- struct List< void, void > {};
- typedef List< void, void > EmptyList;
- EmptyList Nil;
- template <typename A, typename B, typename C>
- List< A, List< B, C > > operator + ( List< B, C > b, A a ){
- List< A, List< B, C > > lst;
- lst.head = a;
- lst.tail = b;
- return lst;
- }
- int main(){
- typedef List<int, List<const char*, EmptyList> > first_t;
- first_t lst1 = Nil + "hello" + 5;
- first_t::head_t a = lst1.head;
- typedef first_t::tail_t second_t;
- second_t lst2 = lst1.tail;
- typedef List<double, second_t> third_t;
- third_t lst3 = lst2 + 0.5;
- third_t::head_t b = lst3.head;
- third_t::tail_t::head_t c = lst3.tail.head;
- std::cout << a << '\n' << b << '\n' << c;
- std::cin.get();
- }
- /*
- same with 'auto'
- int main(){
- auto lst1 = Nil + "hello" + 5;
- auto a = lst1.head;
- auto lst2 = lst1.tail;
- auto lst3 = lst2 + 0.5;
- auto c = lst3.tail.head;
- std::cout << a << '\n' << b << '\n' << c;
- std::cin.get();
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment