From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2001-12-17 15:42:27


Emily Winch wrote:
> for_each for tuples makes sense. for_each for typelists doesn't.

Actually it does :), although I am not talking about current (deprecated)
version of 'for_each' in boost::mpl - as I already said, it should be called
'accumulate'. For example, consider a compile-time computation with results
that need to be passed to a run-time part of the program:

    // fixed-point algorithm input
    typedef mpl::type_vector<
          mpl::fixed_t<-1,2345678>
        , mpl::fixed_t<9,0001>
        // ..
        , mpl::fixed_t<3,14159>
> input_data;
    
    /*
    complex compile-time algorithm skipped :)
    */
    typedef /*...*/ result_data;

    namespace aux {
    struct push_back
    {
        template<typename T> struct apply
        {
            template<typename C> void operator()(C& c)
            {
                // in our case T() == fixed_t() == fixed_t().operator()()
                c.push_back(T());
            }
        };
    };
    }

    // passing the results to the run-time part of the program
    std::vector<double> results;
    typedef mpl::for_each<result_data, aux::push_back> copy_results;
    copy_results::execute(results); // here! :)

    // using results in run-time world
    std::copy(
          results.begin()
        , results.end()
        , std::ostream_iterator<double>(std::cout, "\n"));
        );

There are other ways to generate an analogous code, but IMO they are much
less expressive.

Aleksey