Functors and Boost::MPL

It occurs to me that it should be possible to manage an mpl::vector of types which are actually functors. One could add functors to this list with compile time complexity and likewise call all the functors in the list with compile time complexity. Requiring none of the function pointers, class templates, or virtual functions typically used for this purpose. 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? -Dan "The only impossible task is the task you never start"

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
participants (2)
-
Dan
-
Daniel Wallin