bind_visit_test.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. # pragma warning(disable: 4100) // unreferenced formal parameter (it is referenced!)
  8. #endif
  9. // Copyright (c) 2006 Douglas Gregor <doug.gregor@gmail.com>
  10. // Copyright (c) 2006 Peter Dimov
  11. //
  12. // Distributed under the Boost Software License, Version 1.0. (See
  13. // accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #include <boost/bind.hpp>
  16. #include <boost/visit_each.hpp>
  17. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  18. # pragma warning(push, 3)
  19. #endif
  20. #include <iostream>
  21. #include <typeinfo>
  22. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  23. # pragma warning(pop)
  24. #endif
  25. #include <boost/detail/lightweight_test.hpp>
  26. struct visitor
  27. {
  28. int hash;
  29. visitor(): hash( 0 )
  30. {
  31. }
  32. template<typename T> void operator()( T const & t )
  33. {
  34. std::cout << "visitor::operator()( T ): " << typeid( t ).name() << std::endl;
  35. }
  36. void operator()( int const & t )
  37. {
  38. std::cout << "visitor::operator()( int ): " << t << std::endl;
  39. hash = hash * 10 + t;
  40. }
  41. };
  42. int f( int x, int y, int z )
  43. {
  44. return x + y + z;
  45. }
  46. int main()
  47. {
  48. visitor vis;
  49. boost::visit_each( vis, boost::bind( f, 3, _1, 4 ) );
  50. BOOST_TEST( vis.hash == 34 );
  51. return boost::report_errors();
  52. }