Back to Arangodb

BOOST_PP_WHILE

3rdParty/boost/1.78.0/libs/preprocessor/doc/ref/while.html

3.12.9.13.2 KB
Original Source

The BOOST_PP_WHILE macro represents a looping construct.

Usage

BOOST_PP_WHILE (pred, op, state)

Arguments

pred A binary predicate of the form pred(d, state). This predicate is expanded by BOOST_PP_WHILE with the next available iteration d and the current state. This predicate must expand to value in the range of 0 to BOOST_PP_LIMIT_MAG. The construct continues to loop until this predicate returns 0. When this predicate returns 0, BOOST_PP_WHILE returns the current state. op A binary operation of the form op(d, state). This operation is expanded by BOOST_PP_WHILE with the next available iteration d and the current state. This macro is repeatedly applied to the state, each time producing a new state, until pred returns 0. state The initial state. Often this argument is a tuple.

Remarks

This macro iterates op(d, state) while pred(d, state) is non-zero. In other words expands to: op(d, ... op(d, op(d, state)) ... ).

The d value that is passed to both pred and op represents the next available iteration. Other macros that have _D suffix variants internally use BOOST_PP_WHILE --for example, BOOST_PP_ADD and BOOST_PP_ADD_D. Using these _D versions is not strictly necessary, but passing the d value (that passed to pred or op) to these macros allows them to reenter BOOST_PP_WHILE with maximum efficiency.

To directly use this d value, rather than simply passing it to another macro, see BOOST_PP_WHILE_d.

Previously, this macro could not be used recursively inside BOOST_PP_WHILE. This limitation no longer exists, as the library can automatically detect the next available iteration.

See Also

Requirements

Header: <boost/preprocessor/control/while.hpp>

Sample Code

#include <[boost/preprocessor/arithmetic/add.hpp](../headers/arithmetic/add.html)>
#include <[boost/preprocessor/control/while.hpp](../headers/control/while.html)>
#include <[boost/preprocessor/tuple/elem.hpp](../headers/tuple/elem.html)>

#define PRED(n, state)[BOOST\_PP\_TUPLE\_ELEM](tuple_elem.html)(2, 1, state)

#define OP(d, state) \
   OP_D( \
      d, \[BOOST\_PP\_TUPLE\_ELEM](tuple_elem.html)(2, 0, state), \[BOOST\_PP\_TUPLE\_ELEM](tuple_elem.html)(2, 1, state) \
   ) \
   /**/

#define OP_D(d, res, c) \
   ( \[BOOST\_PP\_ADD\_D](add_d.html)( \
         d, \
         res, \[BOOST\_PP\_DEC](dec.html)(c) \
      ), \[BOOST\_PP\_DEC](dec.html)(c) \
   ) \
   /**/

#define SUMMATION(n) \[BOOST\_PP\_TUPLE\_ELEM](tuple_elem.html)( \
      2, 0, \[BOOST\_PP\_WHILE](while.html)(PRED, OP, (n, n)) \
   ) \
   /**/

SUMMATION(3) // expands to 6
SUMMATION(4) // expands to 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)