c++boost.gif (8819 bytes)

#include <boost/preprocessor/while.hpp>


#define BOOST_PP_WHILE(C,F,X)

Iterates F(D,X) while C(D,X) is true.

In other words, expands to:

  F(D, ... F(D, F(D,X) ) ... )

The depth of iteration is determined by C(D,X).

Legend

NOTE: The value of the D parameter may exceed BOOST_PP_LIMIT_MAG.

Caveat

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

For a more complex example, let's take a look at an implementation of BOOST_PP_MUL().

  #define BOOST_PP_MUL(X,Y) BOOST_PP_MUL_D(0,X,Y)
  // Since the macro is implemented using WHILE, the actual implementation
  // takes a depth as a parameter so that it can be called inside a WHILE.
  // The above easy-to-use version simply uses 0 as the depth and can not be
  // called inside a WHILE.

  #define BOOST_PP_MUL_D(D,X,Y)\
    BOOST_PP_TUPLE_ELEM(3,0,BOOST_PP_WHILE##D(BOOST_PP_MUL_C,BOOST_PP_MUL_F,(0,X,Y)))
  //                    ^^^               ^^^             ^^             ^^ ^^^^^^^
  //                    #1                #2              #3             #3 #1
  //
  // #1) The state is a 3-tuple. After the iteration is finished, the first
  // element of the tuple is the result.
  //
  // #2) The WHILE primitive is "invoked" directly. BOOST_PP_WHILE(D,...)
  // can't be used because it would not be expanded by the C++ preprocessor.
  //
  // #3) ???_C is the condition and ???_F is the iteration macro.

  #define BOOST_PP_MUL_C(D,P)\
    BOOST_PP_TUPLE_ELEM(3,2,P)
  // Iteration is finished when the counter reaches 0.

  #define BOOST_PP_MUL_F(D,P)\
    ( BOOST_PP_ADD_D(D,BOOST_PP_TUPLE_ELEM(3,0,P),BOOST_PP_TUPLE_ELEM(3,1,P))\
    , BOOST_PP_TUPLE_ELEM(3,1,P)\
    , BOOST_PP_DEC(BOOST_PP_TUPLE_ELEM(3,2,P))\
    )
  // ( The result is increased by the multiplier.
  // , The multiplier is retained without change.
  // , The counter is decreased.
  // )

Implementation rationale


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

Generated: