Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2007-11-22 13:03:23


on Wed Nov 21 2007, Roman Perepelitsa <roman.perepelitsa-AT-db.com> wrote:

> I need metafunction cross_product, which should
> transform 2 sequences to a sequence of pairs.
>
> Example:
>
> typedef list<int, char> l1;
> typedef list<int *, char *> l2;
> typedef cross_product<l1, l2>::type c;
> typedef list<
> pair<int, int *>,
> pair<int, char *>,
> pair<char, int *>,
> pair<char, char *>
>> e;
> BOOST_MPL_ASSERT((equal<c, e>));
>
> The best definition I came up with is the
> following:
>
> template <class T, class R>
> struct product : transform<R, pair<T, _1> >
> {
> };
>
> template <class R1, class R2>
> struct cross_product :
> fold<
> typename transform<R1, product<_1, R2> >::type,
> list<>,
> joint_view<_1, _2>
> >
> {
> };
>
> Is there a better way to do it?

Depends what your criteria for "better-ness" are.

> How can I get rid of
> 'struct product' and implement it as one metafunction?

If you're just trying to minimize the amount of code, the above is
pretty good.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

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