Guest User

Untitled

a guest
Aug 18th, 2011
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <typename H, typename T>
  4. struct List{
  5.    H head;
  6.    T tail;
  7.  
  8.    typedef H head_t;
  9.    typedef T tail_t;
  10. };
  11.  
  12. template<>
  13. struct List< void, void > {};
  14.  
  15. typedef List< void, void > EmptyList;
  16. EmptyList Nil;
  17.  
  18. template <typename A, typename B, typename C>
  19. List< A, List< B, C > > operator + ( List< B, C > b, A a ){
  20.    List< A, List< B, C > > lst;
  21.    lst.head = a;
  22.    lst.tail = b;
  23.    return lst;
  24. }
  25.  
  26. int main(){
  27.    typedef List<int, List<const char*, EmptyList> > first_t;
  28.    first_t lst1 = Nil + "hello" + 5;
  29.    
  30.    first_t::head_t a = lst1.head;
  31.    
  32.    typedef first_t::tail_t second_t;
  33.    second_t lst2 = lst1.tail;
  34.  
  35.    typedef List<double, second_t> third_t;
  36.    third_t lst3 = lst2 + 0.5;
  37.  
  38.    third_t::head_t b = lst3.head;
  39.    third_t::tail_t::head_t c = lst3.tail.head;
  40.  
  41.    std::cout << a << '\n' << b << '\n' << c;
  42.    std::cin.get();
  43. }
  44.  
  45. /*
  46. same with 'auto'
  47.  
  48. int main(){
  49.    auto lst1 = Nil + "hello" + 5;
  50.    
  51.    auto a = lst1.head;
  52.    
  53.    auto lst2 = lst1.tail;
  54.  
  55.    auto lst3 = lst2 + 0.5;
  56.  
  57.    auto c = lst3.tail.head;
  58.  
  59.    std::cout << a << '\n' << b << '\n' << c;
  60.    std::cin.get();
  61. }
  62. */
Advertisement
Add Comment
Please, Sign In to add comment