1 
2 // Copyright Aleksey Gurtovoy 2000-2004
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // See http://www.boost.org/libs/mpl for documentation.
9 
10 // $Id$
11 // $Date$
12 // $Revision$
13 
14 #include <boost/mpl/for_each.hpp>
15 
16 #include <boost/mpl/list.hpp>
17 #include <boost/mpl/range_c.hpp>
18 #include <boost/mpl/identity.hpp>
19 #include <boost/mpl/lambda.hpp>
20 #include <boost/bind.hpp>
21 
22 #include <vector>
23 #include <iostream>
24 #include <algorithm>
25 #include <typeinfo>
26 #include <cassert>
27 
28 namespace mpl = boost::mpl;
29 
30 struct type_printer
31 {
type_printertype_printer32     type_printer(std::ostream& s) : f_stream(&s) {}
operator ()type_printer33     template< typename U > void operator()(mpl::identity<U>)
34     {
35         *f_stream << typeid(U).name() << '\n';
36     }
37 
38  private:
39     std::ostream* f_stream;
40 };
41 
42 struct value_printer
43 {
value_printervalue_printer44     value_printer(std::ostream& s) : f_stream(&s) {}
operator ()value_printer45     template< typename U > void operator()(U x)
46     {
47         *f_stream << x << '\n';
48     }
49 
50  private:
51     std::ostream* f_stream;
52 };
53 
54 #ifdef __ICL
55 # pragma warning(disable:985)
56 #endif
57 
push_back(std::vector<int> * c,int i)58 void push_back(std::vector<int>* c, int i)
59 {
60     c->push_back(i);
61 }
62 
main()63 int main()
64 {
65     typedef mpl::list<char,short,int,long,float,double> types;
66     mpl::for_each< types,mpl::make_identity<mpl::_1> >(type_printer(std::cout));
67 
68     typedef mpl::range_c<int,0,10> numbers;
69     std::vector<int> v;
70 
71     mpl::for_each<numbers>(
72           boost::bind(&push_back, &v, _1)
73         );
74 
75     mpl::for_each< numbers >(value_printer(std::cout));
76 
77     for (unsigned i = 0; i < v.size(); ++i)
78         assert(v[i] == (int)i);
79 
80     return 0;
81 }
82