Boost logo

Boost Users :

From: Edd Dawson (edd_at_[hidden])
Date: 2006-08-20 15:54:09


Hello all,

I have the following:

     template<typename T>
     void f() { /* ... */ }

     typedef boost::mpl::vector<int, long, double> my_vec;

I'm looking for a way to call f<T>() for all types T in my_vec. I initially
hoped that either an mpl algorithm or fusion::for_each might be able to
accomplish this for me, but after reading through their docs this doesn't appear
to be the case.

Am I correct in this conclusion? If not, I'd really appreciate someone giving me
a quick pointer as to what I need to do.

Currently I'm using the specialization/static-recursion trick. Something like:

     template<typename types, int idx, int size>
     struct helper
     {
         static void call_f()
         {
             using namespace boost::mpl;
             f< at<types, int_<idx>::type >::type >();
             helper<types, idx + 1, size>::call_f(); // recurse
         }
     };

     template<typename types, int size>
     struct helper<types, size, size>
     {
         static void call_f() { } // end recursion
     };

     void call_f_with_each_type()
     {
         enum { num_types = size<my_vec>::type::value };
         helper<my_vec, 0, num_types>::call_f();
     }

It works fine, but I'm wondering if there's a more succinct and readable way.

Kind regards,

Edd


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net