| 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 | |
| 21 | template<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 | |
| 33 | V(): s_( 0 ) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | template< class T > void operator()( T const & /*t*/ ) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void operator()( int const & v ) |
| 42 | { |
| 43 | s_ = s_ * 10 + v; |
| 44 | } |
| 45 | |
| 46 | void operator()( std::string const & w ) |
| 47 | { |
| 48 | s_ = s_ * 10 + static_cast<int>( w.size() ); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | int main() |
| 53 | { |
| 54 | X x = { .v: 5, .w: "test" }; |
| 55 | V v; |
| 56 | |
| 57 | { |
| 58 | using boost::visit_each; |
| 59 | visit_each( visitor&: v, x ); |
| 60 | } |
| 61 | |
| 62 | BOOST_TEST( v.s_ == 54 ); |
| 63 | |
| 64 | return boost::report_errors(); |
| 65 | } |
| 66 | |