• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3	<title>BOOST_PP_WHILE</title>
4	<link rel="stylesheet" type="text/css" href="../styles.css">
5</head>
6<body>
7	<div style="margin-left:  0px;">
8		The <b>BOOST_PP_WHILE</b> macro represents a looping construct.
9	</div>
10	<h4>Usage</h4>
11		<div class="code">
12			<b>BOOST_PP_WHILE</b>(<i>pred</i>, <i>op</i>, <i>state</i>)
13		</div>
14	<h4>Arguments</h4>
15		<dl>
16			<dt>pred</dt>
17			<dd>
18				A binary predicate of the form <i>pred</i>(<i>d</i>, <i>state</i>).&nbsp;
19				This predicate is expanded by <b>BOOST_PP_WHILE</b> with the next available
20				iteration <i>d</i> and the current <i>state</i>.&nbsp;
21				This predicate must expand to value in the range of <i>0</i> to <b>BOOST_PP_LIMIT_MAG</b>.
22				The construct continues to loop until this predicate returns <i>0</i>.&nbsp;
23				When this predicate returns <i>0</i>, <b>BOOST_PP_WHILE</b> returns the current <i>state</i>.
24			</dd>
25			<dt>op</dt>
26			<dd>
27				A binary operation of the form <i>op</i>(<i>d</i>, <i>state</i>).&nbsp;
28				This operation is expanded by <b>BOOST_PP_WHILE</b> with the next available
29				iteration <i>d</i> and the current <i>state</i>.&nbsp;
30				This macro is repeatedly applied to the <i>state</i>, each time producing a new <i>state</i>, until <i>pred</i> returns <i>0</i>.
31			</dd>
32			<dt>state</dt>
33			<dd>
34				The initial state.&nbsp;
35				Often this argument is a <i>tuple</i>.
36			</dd>
37		</dl>
38	<h4>Remarks</h4>
39		<div>
40			This macro iterates <i>op</i>(<i>d</i>, <i>state</i>) while <i>pred</i>(<i>d</i>, <i>state</i>) is non-zero.&nbsp;
41			In other words expands to:
42			<div>
43				<i>op</i>(<i>d</i>, ... <i>op</i>(<i>d</i>, <i>op</i>(<i>d</i>, <i>state</i>)) ... ).
44			</div>
45		</div>
46		<div>
47			The <i>d</i> value that is passed to both <i>pred</i> and <i>op</i> represents the next available iteration.&nbsp;
48			Other macros that have <b>_D</b> suffix variants internally use <b>BOOST_PP_WHILE</b>--for example, <b>BOOST_PP_ADD</b> and <b>BOOST_PP_ADD_D</b>.&nbsp;
49			Using these <b>_D</b> versions is not strictly necessary, but passing the <i>d</i> value (that passed to <i>pred</i> or <i>op</i>) to these macros allows them to reenter <b>BOOST_PP_WHILE</b> with maximum efficiency.
50		</div>
51		<div>
52			To directly use this <i>d</i> value, rather than simply passing it to another macro, see <b>BOOST_PP_WHILE_<i>d</i></b>.
53		</div>
54		<div>
55			Previously, this macro could not be used recursively inside <b>BOOST_PP_WHILE</b>.&nbsp;
56			This limitation no longer exists, as the library can automatically detect the next available iteration.
57		</div>
58	<h4>See Also</h4>
59		<ul>
60			<li><a href="limit_mag.html">BOOST_PP_LIMIT_MAG</a></li>
61			<li><a href="while_d.html">BOOST_PP_WHILE_<i>d</i></a></li>
62		</ul>
63	<h4>Requirements</h4>
64		<div>
65			<b>Header:</b> &nbsp;<a href="../headers/control/while.html">&lt;boost/preprocessor/control/while.hpp&gt;</a>
66		</div>
67	<h4>Sample Code</h4>
68<div><pre>
69#include &lt;<a href="../headers/arithmetic/add.html">boost/preprocessor/arithmetic/add.hpp</a>&gt;
70#include &lt;<a href="../headers/control/while.html">boost/preprocessor/control/while.hpp</a>&gt;
71#include &lt;<a href="../headers/tuple/elem.html">boost/preprocessor/tuple/elem.hpp</a>&gt;
72
73#define PRED(n, state) <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(2, 1, state)
74
75#define OP(d, state) \
76   OP_D( \
77      d, \
78      <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(2, 0, state), \
79      <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>(2, 1, state) \
80   ) \
81   /**/
82
83#define OP_D(d, res, c) \
84   ( \
85      <a href="add_d.html">BOOST_PP_ADD_D</a>( \
86         d, \
87         res, \
88         <a href="dec.html">BOOST_PP_DEC</a>(c) \
89      ), \
90      <a href="dec.html">BOOST_PP_DEC</a>(c) \
91   ) \
92   /**/
93
94#define SUMMATION(n) \
95   <a href="tuple_elem.html">BOOST_PP_TUPLE_ELEM</a>( \
96      2, 0, \
97      <a href="while.html">BOOST_PP_WHILE</a>(PRED, OP, (n, n)) \
98   ) \
99   /**/
100
101SUMMATION(3) // expands to 6
102SUMMATION(4) // expands to 10
103</pre></div>
104	<hr size="1">
105	<div style="margin-left: 0px;">
106		<i>� Copyright <a href="http://www.housemarque.com" target="_top">Housemarque Oy</a> 2002</i>
107		</br><i>� Copyright Paul Mensonides 2002</i>
108	</div>
109	<div style="margin-left: 0px;">
110		<p><small>Distributed under the Boost Software License, Version 1.0. (See
111		accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
112		copy at <a href=
113		"http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</small></p>
114	</div>
115</body>
116</html>
117