Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/bind/bind_visitor.cpp @ 28

Last change on this file since 28 was 12, checked in by landauf, 17 years ago

added boost

File size: 1.9 KB
Line 
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
36namespace 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
46int hash = 0;
47
48struct 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
63int f(int & i, int & j, int)
64{
65    ++i;
66    --j;
67    return i + j;
68}
69
70int x = 2;
71int y = 7;
72
73int 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
87int 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}
Note: See TracBrowser for help on using the repository browser.