Boost logo

Boost :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2004-07-14 03:59:27


Dan wrote:
> It occurs to me that it should be possible to manage an mpl::vector of
> types which are actually functors.
[snip]
> I think it should be easy to create the vector, but how does one
> construct an object of each type and call operator() on it?

This works:

   #include <boost/mpl/vector.hpp>
   #include <boost/mpl/for_each.hpp>
   #include <boost/bind.hpp>
   #include <boost/bind/apply.hpp>

   struct A
   {
       void operator()();
   };

   struct B
   {
       void operator()();
   };

   ...

   typedef mpl::vector<A, B> v;
   mpl::for_each<v>(boost::apply<void>());

And you could use boost::bind to pass parameters to the functors:

   struct A
   {
       void operator()(int);
   };

   struct B
   {
       void operator()(int);
   };

   ...

   typedef mpl::vector<A, B> v;
   mpl::for_each<v>(
       boost::bind(boost::apply<void>(), _1, 0)
   );

HTH,

-- 
Daniel Wallin

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