Boost logo

Boost :

Subject: Re: [boost] [mpl] Using transform problem and solution
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-01-27 21:15:10


AMDG

On 1/27/2011 6:04 PM, Edward Diener wrote:
> I have a boost::mpl::vector of nullary metafunctions and I need to
> transform it into a sequence of the metafunction's inner types. As in:
>
> typedef boost::mpl::vector<boost::mpl::identity<int> > tvec;
> typedef boost::mpl::vector<int> tvecinner;
>
> So I try boost::mpl::transform:
>
> typedef typename boost::mpl::transform<tvec,_1>::type ttrans;
> BOOST_MPL_ASSERT((boost::mpl::equal<tvecinner,ttrans>));
>
> But that does not work and the assert fails. Evidently my _1 is not
> working by itself although it seems as if it should because it is a
> placeholder expression and its inner type is what I want.
>

I would say that the behavior of MPL is correct.
Consider what would happen with the runtime Lambda:

_1(f) -> f
bind(_1)(f) -> f()

Here also, using _1 by itself isn't
enough to trigger evaluation.

> So I try:
>
> template <class T>
> struct tself : T
> {
> };
>
> and now instead:
>
> typedef typename boost::mpl::transform<tvec,tself<_1> >::type ttrans;
> BOOST_MPL_ASSERT((boost::mpl::equal<tvecinner,ttrans>));
>
> works fine and the assert succeeds.
>
> But that 'tself' hack seems like it should be really unnecessary. Have I
> missed something cleaner and easier ?
>

mpl::apply<_1>?

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk