# copy_backward_if

### Synopsis

    template<
          typename Sequence
        , typename State
        , typename BinaryOp
        , typename Pred
        >
    struct copy_backward_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 reverse order. A typical application for `copy_backward_if` is to conditionally copy the content of one sequence into another - see the example below.

### Definition

    #include "boost/mpl/copy_backward_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 backward traversal.
`Pred` | An unary Predicate [Lambda Expression] | The copying condition.

### Expression semantics

Expression | Expression type | Precondition | Semantics | Postcondition
--- | --- | --- | --- | ---
`typedef copy_backward_if<Sequence,T,Op,Pred>::type s;` | A type |  | Equivalent to `typedef lambda<Op>::type op; typedef lambda<Pred>::type pred; typedef fold_backward< 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_backward_if<
          numbers
        , list_c<int>
        , push_front<_1,_2>
        , less<_1,int_<5> >
        >::type result;

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

### See also

Algorithms, `copy_backward`, `copy_if`, `copy`, `fold`, `fold_backward`

Table of Contents
Last edited March 12, 2003 6:29 am