###  | # Boost.Preprocessor ## Header <boost/preprocessor/while.hpp>

Prev Next Macros Headers

### #define BOOST_PP_WHILE(PRED,OP,STATE)

Iterates `OP(D,STATE)` while `PRED(D,STATE)` is true.

In other words, expands to:

    OP(D, ... OP(D, OP(D,STATE) ) ... )

The depth of iteration is determined by `PRED(D,STATE)`.

For example,

    #define PRED(D,STATE) BOOST_PP_LESS_D(D,BOOST_PP_TUPLE_ELEM(2,0,STATE),BOOST_PP_TUPLE_ELEM(2,1,STATE))
    #define OP(D,STATE) (BOOST_PP_INC(BOOST_PP_TUPLE_ELEM(2,0,STATE)),BOOST_PP_TUPLE_ELEM(2,1,STATE))
    BOOST_PP_WHILE(PRED,OP,(0,3))

expands to:

    (3,3)

### Legend

- STATE is the current state of iteration. The state is usually a tuple.
- PRED is the condition for iteration. It must expand to a decimal integer literal.
- OP is the iterated macro. Note that if the state is a tuple, then OP(D,STATE) usually expands to a tuple of the same number of elements.
- D is the recursion depth and should only be used as a parameter to other macros using BOOST_PP_WHILE(). Such macros include BOOST_PP_ADD() and other arithmetic operations. For each macro using BOOST_PP_WHILE(), there is a version of the macro, distinguished by the D suffix (e.g. BOOST_PP_ADD_D()), 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.

### Automatic recursion?

BOOST_PP_WHILE() currently does not implement automatic recursion. The reason for this is that it would lead to very poor performance. The automatic recursion technique takes O(N) steps just to find out that the Nth recursion should be used. This would dramatically effect the time complexity of macros using automatic recursion.

### Note

- The value of the D parameter may exceed BOOST_PP_LIMIT_MAG.
- Using BOOST_PP_WHILE() is a bit tricky. This is due to the C++ preprocessor limitations. It is recommended to take a look at the implementations of the various PREPROCESSOR library primitives such as BOOST_PP_ADD() for additional examples.

### Example

- count_down.c
- linear_fib.c
- delay.c

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.