visit_each_test.cpp 1021 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // visit_each_test.cpp
  3. //
  4. // Copyright 2014 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. //
  10. #include <boost/visit_each.hpp>
  11. #include <boost/core/lightweight_test.hpp>
  12. #include <string>
  13. struct X
  14. {
  15. int v;
  16. std::string w;
  17. };
  18. template<class Visitor> inline void visit_each( Visitor & visitor, X const & x )
  19. {
  20. using boost::visit_each;
  21. visit_each( visitor, x.v );
  22. visit_each( visitor, x.w );
  23. }
  24. struct V
  25. {
  26. int s_;
  27. V(): s_( 0 )
  28. {
  29. }
  30. template< class T > void operator()( T const & t )
  31. {
  32. }
  33. void operator()( int const & v )
  34. {
  35. s_ = s_ * 10 + v;
  36. }
  37. void operator()( std::string const & w )
  38. {
  39. s_ = s_ * 10 + w.size();
  40. }
  41. };
  42. int main()
  43. {
  44. X x = { 5, "test" };
  45. V v;
  46. {
  47. using boost::visit_each;
  48. visit_each( v, x );
  49. }
  50. BOOST_TEST( v.s_ == 54 );
  51. return boost::report_errors();
  52. }