[mpl] cross product of 2 sequences
Hello, 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? How can I get rid of 'struct product' and implement it as one metafunction? Regards, Roman Perepelitsa Deutsche Bank Moscow +7 (495) 660-74-08 --- This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures.
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
David Abrahams <dave <at> boost-consulting.com> writes:
I need metafunction cross_product, which should transform 2 sequences to a sequence of pairs. ... 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.
Thanks you. That's what I wanted to hear. Regards, Roman Perepelitsa.
participants (2)
-
David Abrahams
-
Roman Perepelitsa