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

The MPL Reference Manual: for_each
Front Page / Algorithms / Runtime Algorithms / for_each

for_each

Synopsis

template<
      typename Sequence
    , typename F
    >
void for_each( F f );

template<
      typename Sequence
    , typename TransformOp
    , typename F
    >
void for_each( F f );

Description

for_each is a family of overloaded function templates:

  • for_each( f ) applies the runtime function object f to every element in the [begin::type, end::type) range.
  • for_each( f ) applies the runtime function object f to the result of the transformation TransformOp of every element in the [begin::type, end::type) range.

Parameters

Parameter Requirement Description
Sequence Forward Sequence A sequence to iterate.
TransformOp Lambda Expression A transformation.
f An unary function object A runtime operation to apply.

Expression semantics

For any Forward Sequence s, Lambda Expression op , and an unary function object f:

for_each( f );
Return type:

void

Postcondition:

Equivalent to

typedef begin::type i1;
value_initialized< deref1>::type > x1;
f(boost::get(x1));

typedef next1>::type i2;
value_initialized< deref2>::type > x2;
f(boost::get(x2));
...
value_initialized< derefn>::type > xn;
f(boost::get(xn));
typedef nextn>::type last;

where n == size::value and last is identical to end::type; no effect if empty::value == true.

for_each( f );
Return type:

void

Postcondition:

Equivalent to

for_each< transform_view >( f );

Complexity

Linear. Exactly size::value applications of op and f.

Example

struct value_printer
{
    template< typename U > void operator()(U x)
    {
        std::cout << x << '\n';
    }
};

int main()
{
    for_each< range_c >( value_printer() );
}