1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <iostream>
#include <string>
#include <list>
struct NullPointer{
NullPointer *data;
NullPointer *next;
};
template <typename T, typename NodeT>
struct BadList{
T data;
NodeT *next;
~BadList(){
delete next;
}
};
template <typename T2>
BadList<T2, NullPointer> *New(const T2 &data){
BadList<T2, NullPointer> *ret = new BadList<T2, NullPointer>;
ret->data = data;
ret->next = 0;
return ret;
}
template <typename T2, typename NodeT2>
BadList<T2, NodeT2> *cons(const T2 &data, NodeT2 *list){
BadList<T2, NodeT2> *ret = new BadList<T2, NodeT2>;
ret->data = data;
ret->next = list;
return ret;
}
template <typename T, typename Function>
void iterator(T *list, Function &f){
if (!list)
return;
f(list->data);
iterator(list->next, f);
}
template <typename Function>
void iterator(NullPointer *list, Function &f){}
struct MyFunction{
template <typename T>
void operator()(const T &data){
auto i = data.begin(), e = data.end();
for (; i != e; ++i)
std::cout <<*i<<std::endl;
}
};
int main(){
std::list<int> list1;
std::list<std::string> list2;
std::list<double> list3;
list1.push_back(42);
list1.push_back(84);
list2.push_back("Hello, ");
list2.push_back("World!");
list3.push_back(3.141592);
list3.push_back(2.718281);
auto l = cons(list1, cons(list2, New(list3)));
MyFunction f;
iterator(l, f);
delete l;
}
|