Boost logo

Boost Users :

Subject: Re: [Boost-users] The Placeholders _1 and _2
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-10-08 16:43:44


AMDG

Missing Rainbow wrote:
> Hi All,
>
> I am going through Chapter 3 of the MPL book ("A Deeper Look at
> Metafunctions") and have some questions on the placeholders _1 and _2.
> They are first introduced in the section 3.1.5 as follows:
>
> typename mpl::transform<D1, D2, mpl::minus<_1, _2> >::type
>
> I understand from the context that _1 represents D1 and _2 represents
> D2. From this I understood that _1 will represent the first template
> argument and _2 will represent the 2nd template argument.

Actually transform applies the lambda expression to the elements of the
sequences
pairwise.

mpl::transform<mpl::vector_c<int, 1, 2>, mpl::vector_c<int, 3, 4>,
mpl::plus<_1, _2> >::type

becomes something equivalent to

mpl::vector_c<int, 4, 6>

The meaning of _1 and _2 is determined by what transform does with the
lambda expression.

> This made
> sense in this context. But later I came across the following in
> section 3.3:
>
> template <class X>
> struct two_pointers
> : twice<boost::add_pointer<_1>, X>
> {};
>

_1 means the first argument to the lambda expression.

For instance this resolves to

template<class F, class T>
struct twice : mpl::apply<F, typename mpl::apply<F, X>::type> {};

So two_pointers is equivalent to

template<class X>
struct two_pointers : boost::add_pointer<typename
boost::add_pointer<X>::type> {};

You can see that _1 refers to the first argument to apply.

In Christ,
Steven Watanabe


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