| Line | |
|---|
| 1 | #include <iostream> |
|---|
| 2 | |
|---|
| 3 | using namespace std; |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | struct _EmptyList { |
|---|
| 7 | void write() const {} |
|---|
| 8 | }; |
|---|
| 9 | |
|---|
| 10 | template <typename _Item, typename _Next> |
|---|
| 11 | struct _AddNode { |
|---|
| 12 | typedef _Next Next; |
|---|
| 13 | typedef _Item Item; |
|---|
| 14 | |
|---|
| 15 | const Item item; |
|---|
| 16 | const Next& next; |
|---|
| 17 | |
|---|
| 18 | _AddNode(const Item& _item, const Next& _next) |
|---|
| 19 | : item(_item), next(_next) {} |
|---|
| 20 | |
|---|
| 21 | void write() const { |
|---|
| 22 | next.write(); |
|---|
| 23 | cout << item << ' '; |
|---|
| 24 | } |
|---|
| 25 | }; |
|---|
| 26 | |
|---|
| 27 | template <typename _List = _EmptyList> |
|---|
| 28 | struct _Writer { |
|---|
| 29 | typedef _List List; |
|---|
| 30 | |
|---|
| 31 | const List list; |
|---|
| 32 | |
|---|
| 33 | _Writer(const List& _list = List()) : list(_list) {} |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | template <typename Item> _Writer<_AddNode<Item, List> > add(Item item) const { |
|---|
| 37 | return _Writer<_AddNode<Item, List> >(_AddNode<Item, List>(item, list)); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | void write() const { |
|---|
| 41 | list.write(); |
|---|
| 42 | cout << endl; |
|---|
| 43 | } |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | typedef _Writer<> Writer; |
|---|
| 48 | |
|---|
| 49 | int main() { |
|---|
| 50 | Writer().add(3).add("alpha").add(4.53).write(); |
|---|
| 51 | } |
|---|
Note: See
TracBrowser
for help on using the repository browser.