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
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

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

               mpl::for_each<list_of_types, wrap<mpl::_1> >(


Dave Abrahams
Boost Consulting

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at