Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2007-07-17 10:54:21


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;

HTH,

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com

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