The BOOST_PP_FOR macro represents a generalized horizontal repetition construct.

Usage

BOOST_PP_FOR(state, pred, op, macro)

Arguments

state
The initial state.
pred
A binary predicate of the form pred(r, state).  This macro must expand to an integer in the range of 0 to BOOST_PP_LIMIT_MAGBOOST_PP_FOR repeatedly expands macro while this predicate returns non-zero.  This macro is called with the next available BOOST_PP_FOR repetition and the current state.
op
A binary operation of the form op(r, state).  This operation is expanded by BOOST_PP_FOR with the next available BOOST_PP_FOR repetition and the current state.  This macro is repeatedly applied to the state, each time producing a new state, until pred returns 0.
macro
A binary macro of the form macro(r, state).  This macro is expanded by BOOST_PP_FOR with the next available BOOST_PP_FOR repetition and the current state.  This macro is is repeated by BOOST_PP_FOR until pred returns 0.

Remarks

This macro expands to the sequence:
macro(r, state) macro(r, op(r, state)) ... macro(r, op(r, ... op(r, state) ... ))
The r value that is passed to pred, op, and macro represents the next available BOOST_PP_FOR repetition.  Other macros that have _R suffix variants internally use BOOST_PP_FOR--for example, BOOST_PP_LIST_FOR_EACH and BOOST_PP_LIST_FOR_EACH_R.  Using these _R versions is not strictly necessary, but passing the r value (that is passed to pred, op, and macro) to these macros allows them to reenter BOOST_PP_FOR with maximum efficiency.
To directly use this r value, rather than simply passing it to another macro, see BOOST_PP_FOR_r.
Previously, this macro could not be used recursively inside BOOST_PP_FOR.  This limitation no longer exists, as the library can automatically detect the next available BOOST_PP_FOR repetition.

See Also

Requirements

Header:  <boost/preprocessor/repetition/for.hpp>

Sample Code

#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/comparison/not_equal.hpp>
#include <boost/preprocessor/repetition/for.hpp>
#include <boost/preprocessor/tuple/elem.hpp>

#define PRED(r, state) \
   BOOST_PP_NOT_EQUAL( \
      BOOST_PP_TUPLE_ELEM(2, 0, state), \
      BOOST_PP_INC(BOOST_PP_TUPLE_ELEM(2, 1, state)) \
   ) \
   /**/

#define OP(r, state) \
   ( \
      BOOST_PP_INC(BOOST_PP_TUPLE_ELEM(2, 0, state)), \
      BOOST_PP_TUPLE_ELEM(2, 1, state) \
   ) \
   /**/

#define MACRO(r, state) BOOST_PP_TUPLE_ELEM(2, 0, state)

BOOST_PP_FOR((5, 10), PRED, OP, MACRO) // expands to 5 6 7 8 9 10

© Copyright Housemarque Oy 2002
© Copyright Paul Mensonides 2002

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt)