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-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.
|