Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is an old version of boost. Click here for the latest version's documentation home page.

[Home]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  
SequenceA model of SequenceA sequence to iterate.
StateA typeThe initial state for the first ForwardOp application.
ForwardOpA 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 typeEquivalent 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