Exercises





Exercises

6-0.

Use mpl::copy with a hand-built inserter to write a smallest metafunction that finds the smallest of a sequence of types. That is:

    BOOST_STATIC_ASSERT((
        boost::is_same<
            smallest< mpl::vector<int[2], char, double&> >::type
          , char
        >::value
    ));

Now that you've done it, is it a good way to solve that problem? Why or why not?

6-1.

Rewrite the binary template from section 1.4.1 using one of the MPL sequence iteration algorithms, and write a test program that will only compile if your binary template is working. Compare the amount of code you wrote with the version using handwritten recursion presented in Chapter 1. What characteristics of the problem caused that result?

6-2.

Because std::for_each is the most basic algorithm in the standard library, you may be wondering why we didn't say anything about its compile time counterpart. The fact is that unlike, for example, transform, the algorithm does not have a pure compile time counterpart. Can you offer an explanation for that fact?

6-3.

Write an inserter class template called binary_tree_inserter that employs the tree template from exercise 5-10 to incrementally build a binary search tree:

    typedef mpl::copy<
          mpl::vector_c<int,17,25,10,2,11>
        , binary_tree_inserter< tree<> >
        >::type bst;

    //       int_<17>
    //       /      \
    //    int_<10>  int_<25>
    //     /    \
    // int_<2> int_<11>
    BOOST_STATIC_ASSERT(( mpl::equal<
          inorder_view<bst>
        , mpl::vector_c<int,2,10,11,17,25>
        >::value ));

6-4.

Write an algorithm metafunction called binary_tree_search that performs binary search on trees built using binary_tree_inserter from exercise 6-3.

    typedef binary_tree_search<bst,int_<11> >::type pos1;
    typedef binary_tree_search<bst,int_<20> >::type pos2;
    typedef mpl::end<bst>::type                     end_pos;

    BOOST_STATIC_ASSERT((!boost::is_same< pos1,end_pos >::value));
    BOOST_STATIC_ASSERT((boost::is_same< pos2,end_pos >::value));

6-5*.

List all algorithms in the standard library and compare their set to the set of algorithms provided by MPL. Analyze the differences. What algorithms are named differently? What algorithms have different semantics? What algorithms are missing? Why do you think they are missing?


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows