###  | # Boost.Preprocessor ## Reference

Prev Next Macros Headers

# #include <boost/preprocessor/for.hpp>

## #define BOOST_PP_FOR(X,C,F,I)

Repeats `I(R,X)` and iterates `F(R,X)` while `C(R,X)` is true.

In other words, expands to the sequence:

      I(R,X)  I(R,F(R,X))  I(R,F(R,F(R,X)))  ...  I(R,F(R,F(...F(R,X)...)))

The length of the sequence is determined by `C(R,X)`.

### Legend

- X is the current state of iteration. The state is usually a tuple.
- C is the condition for iteration. It must expand to a decimal integer literal.
- F is the iterated macro. Note that if the state is a tuple, then F(R,X) usually expands to a tuple of the same number of elements.
- I is the state instantiation macro.
- R is the recursion depth and should only be used as a parameter to other macros using BOOST_PP_FOR() or for invoking BOOST_PP_FOR##R() directly. For each macro using BOOST_PP_FOR(), there is a version of the macro, distinguished by the R suffix, that accepts an additional recursion depth as the first parameter. This technique is necessary to avoid recursively expanding the same macro again, which is not permitted by the C++ preprocessor.

### BOOST_PP_REPEAT() vs BOOST_PP_FOR()

BOOST_PP_FOR() is a generalization of BOOST_PP_REPEAT(). This means that BOOST_PP_REPEAT() can be implemented using BOOST_PP_FOR(). However, BOOST_PP_REPEAT() was introduced earlier, is generally easier to use, and is still quite useful on its own.

### 2D and 3D repetition

BOOST_PP_FOR() can be used for multidimensional repetition simply by invoking BOOST_PP_FOR##R() directly.

### Test

- for_test.cpp

Prev Next Macros Headers

Revised

© Copyright Housemarque Oy 2002

Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.