Boost logo

Boost :

From: Peng Yu (pengyu.ut_at_[hidden])
Date: 2007-07-17 11:17:31


On 7/17/07, David Abrahams <dave_at_[hidden]> wrote:
>
> on Tue Jul 17 2007, "Peng Yu" <pengyu.ut-AT-gmail.com> wrote:
>
> > On 7/17/07, David Abrahams <dave_at_[hidden]> wrote:
> >>
> >> on Tue Jul 17 2007, "Peng Yu" <pengyu.ut-AT-gmail.com> wrote:
> >>
> >> > I saw list in mpl. But I'm not familiar with it yet. Would you please
> >> > show me how to do the same thing in mpl as that in my original
> >> > email?
> >>
> >> typedef boost::mpl::list<Rect_, Square_, Triangle_, RightTriangle_> Shapes_;
> >>
> >
> > Hi,
> >
> > I actually meant to define a visitor class for those shapes like the
> > following. Is there anything similar to type_list::head and
> > type_list::tail in mpl::list?
>
> Well, there is, but in general you don't do it that way. You'd want
> to write code that works just as well with
>
> typedef boost::mpl::vector<Rect_, Square_, Triangle_, RightTriangle_> Shapes_;
>
> All those virtual functions are a waste of time; none of them override
> the others. AFAICT all you're really trying to do is generate an
> overload set on the elements of Shapes_.
>
> struct base_visitor
> {
> void visit();
> };
>
> template <class T, class Base>
> struct visitor : Base
> {
> void visit(T&) { /*...*/ }
> using Base::visit;
> };
>
> typedef mpl::fold<Shapes_, base_visitor, visitor<_2,_1> >::type v;

I know _2 and _1 are placeholders. But I'm not very clear how they are
used here. I tried to read the manual for "fold", but its explanation
refers to many other mpl structs.

Could you give me some easy explanation on how this works?

Thanks,
Peng


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