Boost logo

Boost :

From: Hugo Duncan (hugoduncan_at_[hidden])
Date: 2003-10-09 14:34:03


Hugo Duncan <hugoduncan_at_[hidden]> wrote:

> I'm looking for the following MPL functionality and wondering if it
> already exists somewhere.

Ok, so the attatched code allows me to write the following.

struct myfn
{
   template < typename U >
   void operator()(const boost::mpl::identity<U>&)
   {
     std::cout << typeid(U).name() << std::endl;
   }
};

int main()
{
   typedef boost::mpl::vector<double, float> my_typelist;
   for (int i=0; i<2; ++i)
     ieg::switcher<my_typelist>(myfn(),i);
}

The implementation with for_each is very inefficient, using a
local counter. There must be a better way...

BTW, what do you call a class with a templated operator(), like myfn above?


#include #include #include #include #include #include namespace ieg { namespace detail { template struct selector { selector(F f, std::size_t i) : m_f(f), m_select(i), m_item(0) {} template void operator()(const boost::mpl::identity&) { if (m_item==m_select) { typedef typename boost::mpl::lambda::type transform_op; typedef typename boost::mpl::apply::type transf_type; m_f.operator()(transf_type()); } ++m_item; } F m_f; std::size_t m_select; std::size_t m_item; }; } //! exectute a metafunction on an run-time selected element of type sequence template void switcher(F f, std::size_t i) { detail::selector fs(f,i); boost::mpl::for_each >(fs); } template void switcher(F f, std::size_t i) { switcher >(f,i); } }// namespace #include struct myfn { template < typename U > void operator()(const boost::mpl::identity&) { std::cout << typeid(U).name() << std::endl; } }; int main() { typedef boost::mpl::vector my_typelist; for (int i=0; i<2; ++i) ieg::switcher(myfn(),i); }


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