| 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 | // $Source: /cvsroot/boost/boost/libs/mpl/test/for_each.cpp,v $ |
|---|
| 11 | // $Date: 2004/11/28 03:35:12 $ |
|---|
| 12 | // $Revision: 1.13 $ |
|---|
| 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 | { |
|---|
| 32 | type_printer(std::ostream& s) : f_stream(&s) {} |
|---|
| 33 | 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 | { |
|---|
| 44 | value_printer(std::ostream& s) : f_stream(&s) {} |
|---|
| 45 | 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 | |
|---|
| 58 | int main() |
|---|
| 59 | { |
|---|
| 60 | typedef mpl::list<char,short,int,long,float,double> types; |
|---|
| 61 | mpl::for_each< types,mpl::make_identity<mpl::_1> >(type_printer(std::cout)); |
|---|
| 62 | |
|---|
| 63 | typedef mpl::range_c<int,0,10> numbers; |
|---|
| 64 | std::vector<int> v; |
|---|
| 65 | |
|---|
| 66 | #if defined(__SGI_STL_PORT) |
|---|
| 67 | void (std::vector<int>::* push_back)(int const&) = &std::vector<int>::push_back; |
|---|
| 68 | mpl::for_each<numbers>( |
|---|
| 69 | boost::bind(push_back, &v, _1) |
|---|
| 70 | ); |
|---|
| 71 | #else |
|---|
| 72 | mpl::for_each<numbers>( |
|---|
| 73 | boost::bind(&std::vector<int>::push_back, &v, _1) |
|---|
| 74 | ); |
|---|
| 75 | #endif |
|---|
| 76 | |
|---|
| 77 | mpl::for_each< numbers >(value_printer(std::cout)); |
|---|
| 78 | |
|---|
| 79 | for (unsigned i = 0; i < v.size(); ++i) |
|---|
| 80 | assert(v[i] == (int)i); |
|---|
| 81 | |
|---|
| 82 | return 0; |
|---|
| 83 | } |
|---|