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 11 #include <boost/visit_each.hpp> 12 #include <boost/core/lightweight_test.hpp> 13 #include <string> 14 15 struct X 16 { 17 int v; 18 std::string w; 19 }; 20 visit_each(Visitor & visitor,X const & x)21template<class Visitor> inline void visit_each( Visitor & visitor, X const & x ) 22 { 23 using boost::visit_each; 24 25 visit_each( visitor, x.v ); 26 visit_each( visitor, x.w ); 27 } 28 29 struct V 30 { 31 int s_; 32 VV33 V(): s_( 0 ) 34 { 35 } 36 operator ()V37 template< class T > void operator()( T const & t ) 38 { 39 } 40 operator ()V41 void operator()( int const & v ) 42 { 43 s_ = s_ * 10 + v; 44 } 45 operator ()V46 void operator()( std::string const & w ) 47 { 48 s_ = s_ * 10 + w.size(); 49 } 50 }; 51 main()52int main() 53 { 54 X x = { 5, "test" }; 55 V v; 56 57 { 58 using boost::visit_each; 59 visit_each( v, x ); 60 } 61 62 BOOST_TEST( v.s_ == 54 ); 63 64 return boost::report_errors(); 65 } 66