bind_visitor.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <boost/config.hpp>
  2. #if defined(BOOST_MSVC)
  3. #pragma warning(disable: 4786) // identifier truncated in debug info
  4. #pragma warning(disable: 4710) // function not inlined
  5. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  6. #pragma warning(disable: 4514) // unreferenced inline removed
  7. #endif
  8. //
  9. // bind_visitor.cpp - tests bind.hpp with a visitor
  10. //
  11. // Copyright (c) 2001 Peter Dimov and Multi Media Ltd.
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #include <boost/bind.hpp>
  18. #include <boost/ref.hpp>
  19. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  20. #pragma warning(push, 3)
  21. #endif
  22. #include <iostream>
  23. #include <typeinfo>
  24. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  25. #pragma warning(pop)
  26. #endif
  27. //
  28. struct visitor
  29. {
  30. template<class T> void operator()( boost::reference_wrapper<T> const & r ) const
  31. {
  32. std::cout << "Reference to " << typeid(T).name() << " @ " << &r.get() << " (with value " << r.get() << ")\n";
  33. }
  34. template<class T> void operator()( T const & t ) const
  35. {
  36. std::cout << "Value of type " << typeid(T).name() << " (with value " << t << ")\n";
  37. }
  38. };
  39. int f(int & i, int & j, int)
  40. {
  41. ++i;
  42. --j;
  43. return i + j;
  44. }
  45. int x = 2;
  46. int y = 7;
  47. int main()
  48. {
  49. using namespace boost;
  50. visitor v;
  51. visit_each(v, bind<int>(bind(f, ref(x), _1, 42), ref(y)), 0);
  52. }