# fold

### Synopsis

    template<
          typename Sequence
        , typename State
        , typename ForwardOp
        >
    struct fold
    {
        typedef unspecified type;
    };

### Description

Returns the result of the successive application of binary `ForwardOp` to the result of the previous `ForwardOp` invocation (`State` if it's the first call) and every element of the sequence in the range `[begin<Sequence>::type,end<Sequence>::type)` in the linear order.

### Definition

    #include "boost/mpl/fold.hpp"

### Parameters

Parameter | Requirement | Description
--- | --- | ---
`Sequence` | A model of Sequence | A sequence to iterate.
`State` | A type | The initial state for the first `ForwardOp` application.
`ForwardOp` | A model of [Lambda Function] | The operation to be executed on forward traversal.

### Expression semantics

Expression | Expression type | Precondition | Semantics | Postcondition
--- | --- | --- | --- | ---
`typedef fold<Sequence,T,Op>::type t;` | A type |  | Equivalent to `typedef lambda<Op>::type op; typedef iter_fold< Sequence,T,apply<op,_1,deref<_2> > >::type t;`. |

### Complexity

Linear. Exactly `size<Sequence>::type::value` applications of `ForwardOp`.

### Example

    typedef vector<long,float,short,double,float,long,long double> types;
    typedef typename fold<
          types
        , integral_c<long, 0>
        , if_< is_float<_2>,next<_1>,_1 >
        >::type number_of_floats;

    BOOST_STATIC_ASSERT(number_of_floats::value == 4);

### See also

Algorithms, `fold_backward`, `iter_fold`, `iter_fold_backward`, `copy`, `copy_if`

Table of Contents
Last edited December 12, 2002 4:42 pm