# copy_if

### Synopsis

    template<
          typename Sequence
        , typename State
        , typename BinaryOp
        , typename Pred
        >
    struct copy_if
    {
        typedef unspecified type;
    };

### Description

Returns the result of the successive application of `BinaryOp` to the result of the previous `BinaryOp` invocation (`State` if it's the first call) and every element in the range `[begin<Sequence>::type,end<Sequence>::type)` that satisfies the predicate `Pred`, in the linear order. A typical application for `copy_if` is to conditionally copy the content of one sequence into another - see the example below.

### Definition

    #include "boost/mpl/copy_if.hpp"

### Parameters

Parameter | Requirement | Description
--- | --- | ---
`Sequence` | A model of Sequence | A sequence to iterate.
`State` | A type | The initial state for the first `BinaryOp` application.
`BinaryOp` | A model of [Lambda Function] | The operation to be executed on forward traversal.
`Pred` | An unary Predicate [Lambda Expression] | The copying condition.

### Expression semantics

Expression | Expression type | Precondition | Semantics | Postcondition
--- | --- | --- | --- | ---
`typedef copy_if<Sequence,T,Op,Pred>::type s;` | A type |  | Equivalent to `typedef lambda<Op>::type op; typedef lambda<Pred>::type pred; typedef fold< Sequence,T,if_< apply<pred,_2>, apply<op,_1,_2>, _1 > >::type s;`. |

### Complexity

Linear. Exactly `size<Sequence>::type::value` applications of `Pred`, and at most `size<Sequence>::type::value` applications of `BinaryOp`.

### Example

    typedef list_c<int,0,1,2,3,4,5,6,7,8,9>::type numbers;
    typedef list_c<int,0,1,2,3,4>::type answer;
    typedef copy_if<
          numbers
        , vector_c<int>
        , push_back<_,_>
        , less<_,int_c<5> >
        >::type result;

    BOOST_STATIC_ASSERT(size<result>::value == 5);
    BOOST_STATIC_ASSERT((equal<result,answer>::type::value));

### See also

Algorithms, `copy`, `copy_backward_if`, `copy_backward`, `fold`, `iter_fold`

Table of Content | Reference
Last edited July 19, 2002 2:10 am