C++ Boost

Boost.Preprocessor

Header <boost/preprocessor/for.hpp>


Prev Next Macros Headers

#define BOOST_PP_FOR(STATE,PRED,OP,MACRO)

Repeats MACRO(R,STATE) and iterates OP(R,STATE) while PRED(R,STATE) is true.

In other words, expands to the sequence:

MACRO(R,STATE)
MACRO(R,OP(R,STATE))
MACRO(R,OP(R,OP(R,STATE)))
...
MACRO(R,OP(R,OP(...OP(R,STATE)...)))

The length of the sequence is determined by PRED(R,STATE).

For example,

#define PRED(R,STATE) BOOST_PP_LESS(BOOST_PP_TUPLE_ELEM(2,0,STATE),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((0,3),PRED,OP,MACRO)

expands to:

0 1 2

Legend

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.

Automatic recursion?

BOOST_PP_FOR() 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.

Test


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.