Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-08-29 07:53:31


Geoffrey Irving <irving_at_[hidden]> writes:

> Hello,
>
> I would like to be able to write code like this:
>
> int i;
> for F in LIST_OF_TYPES
> complicated_function<F>(i);
>
> where this code is inside some other function which is inside a class
> templatized over LIST_OF_TYPES. Given the lack of nested functions and
> classes in C++ functions, am I correct in thinking that it's impossible
> to do this sort of thing inside another function?

Okay, what's wrong with boost::mpl::for_each? Argh, no wonder! Why
isn't that in the reference manual?

I'm afraid you'll have to read http://www.boost-consulting.com/mplbook
for documentaiton on for_each :(

This general approach should work:

       // Just in case T can't be constructed and passed by value
       template <class T>
       struct wrap {};

       struct call_complicated
       {
           // You could also use boost::bind for this
           call_complicated(int i) : i(i) {}
           int i;

           template <class T>
           void operator()(wrap<T>) const
           {
                complicated_function<T>(i);
           }
       };

       template <class list_of_types>
       class foo
       {
           void some_function()
           {
               int i;

               mpl::for_each<list_of_types, wrap<mpl::_1> >(
                   call_complicated(i)
               );
           }
       };

HTH,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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