| 1 | #include <boost/config.hpp> |
|---|
| 2 | |
|---|
| 3 | #if defined(BOOST_MSVC) |
|---|
| 4 | #pragma warning(disable: 4786) // identifier truncated in debug info |
|---|
| 5 | #pragma warning(disable: 4710) // function not inlined |
|---|
| 6 | #pragma warning(disable: 4711) // function selected for automatic inline expansion |
|---|
| 7 | #pragma warning(disable: 4514) // unreferenced inline removed |
|---|
| 8 | #endif |
|---|
| 9 | |
|---|
| 10 | // |
|---|
| 11 | // bind_visitor.cpp - tests bind.hpp with a visitor |
|---|
| 12 | // |
|---|
| 13 | // Copyright (c) 2001 Peter Dimov and Multi Media Ltd. |
|---|
| 14 | // |
|---|
| 15 | // Distributed under the Boost Software License, Version 1.0. (See |
|---|
| 16 | // accompanying file LICENSE_1_0.txt or copy at |
|---|
| 17 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 18 | // |
|---|
| 19 | |
|---|
| 20 | #include <boost/bind.hpp> |
|---|
| 21 | #include <boost/ref.hpp> |
|---|
| 22 | |
|---|
| 23 | #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) |
|---|
| 24 | #pragma warning(push, 3) |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #include <iostream> |
|---|
| 28 | #include <typeinfo> |
|---|
| 29 | |
|---|
| 30 | #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) |
|---|
| 31 | #pragma warning(pop) |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | // default implementation of visit_each |
|---|
| 35 | |
|---|
| 36 | namespace boost |
|---|
| 37 | { |
|---|
| 38 | template<class V, class T> void visit_each(V & v, T const & t, long) |
|---|
| 39 | { |
|---|
| 40 | v(t, 0); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | // visitor |
|---|
| 45 | |
|---|
| 46 | int hash = 0; |
|---|
| 47 | |
|---|
| 48 | struct visitor |
|---|
| 49 | { |
|---|
| 50 | template<class T> void operator()(boost::reference_wrapper<T> const & r, int) const |
|---|
| 51 | { |
|---|
| 52 | std::cout << "Reference to " << typeid(T).name() << " @ " << &r.get() << " (with value " << r.get() << ")\n"; |
|---|
| 53 | hash += r.get(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | template<class T> void operator()(T const &, long) const |
|---|
| 57 | { |
|---|
| 58 | std::cout << "Value of type " << typeid(T).name() << '\n'; |
|---|
| 59 | ++hash; |
|---|
| 60 | } |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | int f(int & i, int & j, int) |
|---|
| 64 | { |
|---|
| 65 | ++i; |
|---|
| 66 | --j; |
|---|
| 67 | return i + j; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | int x = 2; |
|---|
| 71 | int y = 7; |
|---|
| 72 | |
|---|
| 73 | int detect_errors(bool x) |
|---|
| 74 | { |
|---|
| 75 | if(x) |
|---|
| 76 | { |
|---|
| 77 | std::cerr << "no errors detected.\n"; |
|---|
| 78 | return 0; |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | std::cerr << "test failed.\n"; |
|---|
| 83 | return 1; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | int main() |
|---|
| 88 | { |
|---|
| 89 | using namespace boost; |
|---|
| 90 | |
|---|
| 91 | visitor v; |
|---|
| 92 | visit_each(v, bind<int>(bind(f, ref(x), _1, 42), ref(y)), 0); |
|---|
| 93 | |
|---|
| 94 | return detect_errors(hash == 12); |
|---|
| 95 | } |
|---|