Files
preprocessor/doc/reference/for.htm
Vesa Karvonen def1674ee7 Header info
[SVN r13970]
2002-05-17 21:24:59 +00:00

107 lines
4.8 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../../../boost.css">
<title>Boost.Preprocessor - Reference</title>
</head>
<body link="#0000ff" vlink="#800080">
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
"header">
<tr>
<td valign="top" width="300">
<h3><a href="../../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../../c++boost.gif" border="0"></a></h3>
</td>
<td valign="top">
<h1 align="center">Boost.Preprocessor</h1>
<h2 align="center">Header &lt;<a href="../../../../boost/preprocessor/for.hpp">boost/preprocessor/for.hpp</a>&gt;</h2>
</td>
</tr>
</table>
<hr>
<a href="expr_if.htm">Prev</a> <a href="identity.htm">Next</a> <a href="index.htm#Macros">Macros</a> <a href="index.htm#Headers">Headers</a>
<hr>
<h3><a name="BOOST_PP_FOR">#define BOOST_PP_FOR</a>(STATE,PRED,OP,MACRO)</h3>
<p>Repeats <code>MACRO(R,STATE)</code> and iterates <code>OP(R,STATE)</code> while
<code>PRED(R,STATE)</code> is true.</p>
<p>In other words, expands to the sequence:</p>
<pre>
MACRO(R,STATE)
MACRO(R,OP(R,STATE))
MACRO(R,OP(R,OP(R,STATE)))
...
MACRO(R,OP(R,OP(...OP(R,STATE)...)))
</pre>
<p>The length of the sequence is determined by <code>PRED(R,STATE)</code>.</p>
<p>For example,</p>
<pre>
#define PRED(R,STATE) <a href="comparison_less.htm#BOOST_PP_LESS">BOOST_PP_LESS</a>(<a href="tuple_elem.htm#BOOST_PP_TUPLE_ELEM">BOOST_PP_TUPLE_ELEM</a>(2,0,STATE),<a href="tuple_elem.htm#BOOST_PP_TUPLE_ELEM">BOOST_PP_TUPLE_ELEM</a>(2,1,STATE))
#define OP(R,STATE) (<a href="inc.htm#BOOST_PP_INC">BOOST_PP_INC</a>(<a href="tuple_elem.htm#BOOST_PP_TUPLE_ELEM">BOOST_PP_TUPLE_ELEM</a>(2,0,STATE)),<a href="tuple_elem.htm#BOOST_PP_TUPLE_ELEM">BOOST_PP_TUPLE_ELEM</a>(2,1,STATE))
#define MACRO(R,STATE) <a href="tuple_elem.htm#BOOST_PP_TUPLE_ELEM">BOOST_PP_TUPLE_ELEM</a>(2,0,STATE)
<a href="for.htm#BOOST_PP_FOR">BOOST_PP_FOR</a>((0,3),PRED,OP,MACRO)
</pre>
<p>expands to:</p>
<pre>
0 1 2
</pre>
<h3>Legend</h3>
<ul>
<li><b>STATE</b> is the current state of iteration. The state is usually a tuple.</li>
<li><b>PRED</b> is the condition for iteration. It must expand to a decimal
integer literal.</li>
<li><b>OP</b> is the iterated macro. Note that if the state is a tuple, then
OP(R,STATE) usually expands to a tuple of the same number of elements.</li>
<li><b>MACRO</b> is the state instantiation macro.</li>
<li><b>R</b> is the recursion depth and should only be used as a parameter to
other macros using <a href="for.htm#BOOST_PP_FOR">BOOST_PP_FOR</a>() or for invoking <a href="for.htm#BOOST_PP_FOR">BOOST_PP_FOR</a>##R()
directly. For each macro using <a href="for.htm#BOOST_PP_FOR">BOOST_PP_FOR</a>(), e.g. <a href="list_for_each.htm#BOOST_PP_LIST_FOR_EACH">BOOST_PP_LIST_FOR_EACH</a>(),
there is a version of the macro, e.g. <a href="list_for_each.htm#BOOST_PP_LIST_FOR_EACH_R">BOOST_PP_LIST_FOR_EACH_R</a>(), distinguished
by the R suffix, 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.</li>
</ul>
<h3><a href="repeat.htm#BOOST_PP_REPEAT">BOOST_PP_REPEAT</a>() vs <a href="for.htm#BOOST_PP_FOR">BOOST_PP_FOR</a>()</h3>
<p><a href="for.htm#BOOST_PP_FOR">BOOST_PP_FOR</a>() is a generalization of <a href="repeat.htm#BOOST_PP_REPEAT">BOOST_PP_REPEAT</a>(). This means that
<a href="repeat.htm#BOOST_PP_REPEAT">BOOST_PP_REPEAT</a>() can be implemented using <a href="for.htm#BOOST_PP_FOR">BOOST_PP_FOR</a>(). However,
<a href="repeat.htm#BOOST_PP_REPEAT">BOOST_PP_REPEAT</a>() was introduced earlier, is generally easier to use, and is
still quite useful on its own.</p>
<h3>2D and 3D repetition</h3>
<p><a href="for.htm#BOOST_PP_FOR">BOOST_PP_FOR</a>() can be used for multidimensional repetition simply by
invoking <a href="for.htm#BOOST_PP_FOR">BOOST_PP_FOR</a>##R() directly.</p>
<h3>Test</h3>
<ul>
<li><a href="../../test/for_test.cpp">for_test.cpp</a></li>
</ul>
<hr>
<a href="expr_if.htm">Prev</a> <a href="identity.htm">Next</a> <a href="index.htm#Macros">Macros</a> <a href="index.htm#Headers">Headers</a>
<hr>
<p>Revised <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %b %Y" startspan --><!--webbot bot="Timestamp" endspan i-checksum="15246" --></p>
<p><i>&copy; Copyright <a href="http://www.housemarque.com">Housemarque Oy</a> 2002</i></p>
<p>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.</p>
</body></html>