Here's code:
//solution to excercise 7.8 using transform_view #include <boost/mpl/pair.hpp> #include <boost/mpl/transform_view.hpp> #include <boost/mpl/range_c.hpp> #include <boost/mpl/for_each.hpp> #include <iostream> #include <boost/test/unit_test.hpp> namespace boost{namespace mpl{ template<class Row, class Column> struct row_column_view : pair<Row,Column> { typedef row_column_view type ; static void print(void) { std::cout<<"row.col="<<Row::value<<"."<<Column::value<<"\n"; } }; template<class Row, class Columns> struct row_view : transform_view<Columns,row_column_view<Row, arg<1> > >::type { typedef row_view type ; struct element_printer { template<class RowColumnView> void operator()(RowColumnView) { RowColumnView::print(); } }; }; template<class Rows, class Columns> struct cross_product_view : transform_view<Rows, row_view<arg<1>, Columns> >::type { struct element_printer { template<class RowView> void operator()(RowView) { typedef RowView view_type; typename view_type::element_printer a_printer; for_each<view_type>(a_printer); } }; static void print(void) { typedef cross_product_view view_type; typename view_type::element_printer a_printer; for_each<view_type>(a_printer); } }; }}//exit boost::mpl namespace using namespace boost; void test(void) { typedef mpl::range_c<int,0,3> cols_type; typedef mpl::range_c<int,100,104> rows_type; mpl::cross_product_view<cols_type,rows_type>::print(); } void always_fail(void) { BOOST_CHECK(false); } namespace butf = boost::unit_test_framework; butf::test_suite* init_unit_test_suite(int argc, char* argv[]) { butf::test_suite* tests = BOOST_TEST_SUITE("exercise 7.8 tests"); tests->add(BOOST_TEST_CASE(&test)); tests->add(BOOST_TEST_CASE(&always_fail)); return tests; }
Here's output:
Running 2 test cases... row.col=0.100 row.col=0.101 row.col=0.102 row.col=0.103 row.col=1.100 row.col=1.101 row.col=1.102 row.col=1.103 row.col=2.100 row.col=2.101 row.col=2.102 row.col=2.103 8.xform_view.cpp(86): error in "always_fail": test false failed
This solution by: Larry Evans ( cppljevans@cox-internet.com )