Boost logo

Boost Users :

From: Bruno Martínez Aguerre (br1_at_[hidden])
Date: 2005-03-12 15:36:14


Hi,

I'm *partially* answering to myself.

On Thu, 03 Mar 2005 22:54:56 -0200, Bruno Martínez Aguerre
<br1_at_[hidden]> wrote:

> I'm stuck at exercise 3-6 of the book. I have to write twice (see
> below) as a lambda expression. I don't understand why the same
> transformation that works for quasi fails for twice. Any help, please?
>
> #include <boost/type_traits/add_pointer.hpp>
> #include <boost/type_traits/is_same.hpp>
>
> #include <boost/mpl/assert.hpp>
> #include <boost/mpl/plus.hpp>
> #include <boost/mpl/int.hpp>
> #include <boost/mpl/placeholders.hpp>
> #include <boost/mpl/apply.hpp>
> #include <boost/mpl/lambda.hpp>
> #include <boost/mpl/equal_to.hpp>
>
> using namespace boost;
> using namespace boost::mpl;
> using namespace boost::mpl::placeholders;
>
>
> template <class F, class A>
> struct twice : apply<F, typename apply<F, A>::type > {};
>
> BOOST_MPL_ASSERT(( is_same<twice<add_pointer<_1>,int>::type, int**> ));
>
> template <class F, class A>
> struct quasi : plus<F, typename plus<F, A>::type > {};
>
> BOOST_MPL_ASSERT(( equal_to<quasi<int_<2>,int_<3> >, int_<7> > ));

This works because quasi inherits from plus that inherits from int_.

> template <class F, class A>
> struct quasi2 : apply<plus<_1, plus<_1,_2> >, F, A >::type {};

Here ::type makes quasi2 inherit from int_, too, ...

> BOOST_MPL_ASSERT(( equal_to<quasi2<int_<2>,int_<3> >, int_<7> > ));

... so this works again.

> template <class F, class A>
> struct twice2 : apply<apply<_1, apply<_1,_2> >, F, A >::type {};
>
> BOOST_MPL_ASSERT(( is_same<twice2<add_pointer<_1>,int>::type, int**> ));

This has a number of bugs from copying quasi2. Something that works would
be:

template <class F, class A>
struct twice2 : apply< apply1<_1, apply1<_1, _2> >, F, A > {};

BOOST_MPL_ASSERT(( is_same<twice2<add_pointer<_1>,int>::type, int**> ));

Now, this isn't right yet. If I replace apply1 with apply, it breaks.
Could anybody point out why? The first g++ error message is:

ej3_6.cpp: In instantiation of
`boost::mpl::apply_wrap2<boost::mpl::apply<mpl_::_1,
boost::mpl::apply<mpl_::_1, mpl_::_2, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na>,
boost::add_pointer<mpl_::_1>, int>':
ej3_6.cpp:22: instantiated from
`boost::mpl::apply2<boost::mpl::apply<mpl_::_1,
boost::mpl::apply<mpl_::_1, mpl_::_2, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na>,
boost::add_pointer<mpl_::_1>, int>'
ej3_6.cpp:22: instantiated from
`boost::mpl::apply<boost::mpl::apply<mpl_::_1, boost::mpl::apply<mpl_::_1,
mpl_::_2, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na,
mpl_::na, mpl_::na>, boost::add_pointer<mpl_::_1>, int, mpl_::na,
mpl_::na, mpl_::na>'
ej3_6.cpp:22: instantiated from `twice2<boost::add_pointer<mpl_::_1>,
int>'
ej3_6.cpp:22: instantiated from here
ej3_6.cpp:22: no class template named `apply' in `struct
    boost::mpl::apply<mpl_::_1, boost::mpl::apply<mpl_::_1, mpl_::_2,
mpl_::na,
    mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na>'

Thank you,
Bruno


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net