Section 4.3.  Exercises
Team LiB
Previous Section Next Section

4.3. Exercises

4-0.

Write tests for mpl::or_ and mpl::and_ metafunctions that use their short-circuit behavior.

4-1.

Implement binary metafunctions called logical_or and logical_and that model the behavior of mpl::or_ and mpl::and_, correspondingly. Use tests from exercise 4-0 to verify your implementation.

4-2.

Extend the implementation of logical_or and logical_and metafunctions from exercise 4-1 to accept up to five arguments.

4-3.

Eliminate the unnecessary instantiations in the following code snippets:



1. template< typename N, typename Predicate >


   struct next_if


     : mpl::if_<


           typename mpl::apply<Predicate,N>::type


         , typename mpl::next<N>::type


         , N


       >


   {};


2. template< typename N1, typename N2 >


   struct formula


     : mpl::if_<


           mpl::not_equal_to<N1,N2>


         , typename mpl::if_<


               mpl::greater<N1,N2>


             , typename mpl::minus<N1,N2>::type


             , N1


           >::type


         , typename mpl::plus<


               N1


               , typename mpl::multiplies<N1,


                                       mpl::int_<2> >::type


        >::type


    >::type


{};



Write the tests to verify that the semantics of the transformed metafunctions remained unchanged.

4-4.

Use integral operators and the type traits library facilities to implement the following composite traits:



is_data_member_pointer


is_pointer_to_function


is_reference_to_function_pointer


is_reference_to_non_const



4-5.

Consider the following function template, which is designed to provide a "container-based" (as opposed to iterator-based) interface to std::find:



template <class Container, class Value>


typename Container::iterator


container_find(Container& c, Value const& v)


{


    return std::find(c.begin(), c.end(), v);


}



As coded, container_find won't work for const containers; Container will be deduced as const X for some container type X, but when we try to convert the Container::const_iterator returned by std::find into a Container::iterator, compilation will fail. Fix the problem using a small metaprogram to compute container_find's return type.

    Team LiB
    Previous Section Next Section