3 <title>evaluated_slots.html
</title>
4 <link rel=
"stylesheet" type=
"text/css" href=
"../styles.css">
7 <h4>Evaluated Slots
</h4>
9 The evaluated slot mechanism is a tool to fully evaluate a constant integral expression and avoid the lazy evaluation normally performed by the preprocessor.
13 In order to understand the use of such a mechanism, I will start with a simple file-iteration example.
14 Consider the following scenario....
16 <div class =
"code"><pre>
17 for (int i =
0; i <
10; ++i) {
18 for (int j =
0; j < i; ++j) {
24 The above is a simple runtime model of the following multidimensional file-iteration....
26 <div class=
"code"><pre>
28 #if !BOOST_PP_IS_ITERATING
32 #include
<boost/preprocessor/iteration/iterate.hpp
>
34 #define BOOST_PP_ITERATION_PARAMS_1 (
3, (
0,
9,
"file.hpp"))
35 #include BOOST_PP_ITERATE()
38 #elif BOOST_PP_ITERATION_DEPTH() ==
1
39 #define I BOOST_PP_ITERATION()
41 #define BOOST_PP_ITERATION_PARAMS_2 (
3, (
0, I,
"file.hpp"))
42 #include BOOST_PP_ITERATE()
45 #elif BOOST_PP_ITERATION_DEPTH() ==
2
46 #define J BOOST_PP_ITERATION()
54 There is a problem with the code above.
55 The writer expected
<i>I
</i> to refer the previous iteration frame.
56 However, that is not the case.
57 When the user refers to
<i>I
</i>, he is actually referring to
<b>BOOST_PP_ITERATION
</b>(),
58 not the value of
<b>BOOST_PP_ITERATION
</b>() at the point of definition.
59 Instead, it refers to exactly the same value to which
<i>J
</i> refers.
62 The problem is that the preprocessor always evaluates everything with lazy evaluation.
63 To solve the problem, we need
<i>I
</i> to be
<i>evaluated
</i> here:
65 <div class=
"code"><pre>
67 #elif BOOST_PP_ITERATION_DEPTH() ==
1
68 #define I BOOST_PP_ITERATION()
72 Fortunately, the library offers a mechanism to do just that:
evaluated slots.
73 The following code uses this mechanism to
"fix" the example above...
75 <div class=
"code"><pre>
77 #elif BOOST_PP_ITERATION_DEPTH() ==
1
78 #define BOOST_PP_VALUE BOOST_PP_ITERATION()
79 #include BOOST_PP_ASSIGN_SLOT(
1)
80 #define I BOOST_PP_SLOT(
1)
84 There are two steps to the assignment of an evaluated slot.
85 First, the user must define the
<i>named external argument
</i> <b>BOOST_PP_VALUE
</b>.
86 This value must be an integral constant expression.
87 Second, the user must
<i>include
</i> <b>BOOST_PP_ASSIGN_SLOT
</b>(
<i>x
</i>), where
<i>x
</i> is the particular slot to be assigned to (
<i>1</i> to
<b>BOOST_PP_LIMIT_SLOT_COUNT
</b>).
88 This will evaluate
<b>BOOST_PP_VALUE
</b> and assign the result to the slot at index
<i>x
</i>.
91 To retrieve a slot's value, the user must use
<b>BOOST_PP_SLOT
</b>(
<i>x
</i>).
94 In the case above,
<i>I
</i> is
<i>still
</i> lazily evaluated.
95 However, it now evaluates to
<b>BOOST_PP_SLOT
</b>(
<i>1</i>).
96 This value
<i>will not change
</i> unless there is a subsequent call to
<b>BOOST_PP_ASSIGN_SLOT
</b>(
<i>1</i>).
98 <h4>Advanced Techniques
</h4>
100 The slot mechanism can also be used to perform calculations:
102 <div class=
"code"><pre>
103 #include
<iostream
>
105 #include
<boost/preprocessor/slot/slot.hpp
>
106 #include
<boost/preprocessor/stringize.hpp
>
110 #define BOOST_PP_VALUE
1 +
2 +
3 + X()
111 #include BOOST_PP_ASSIGN_SLOT(
1)
117 << BOOST_PP_STRINGIZE(BOOST_PP_SLOT(
1))
118 << &std::endl;
123 In essence, anything that can be evaluated in an #if (or #elif) preprocessor directive is available
<i>except
</i> the
<i>defined
</i> operator.
126 It is even possible to use a particular slot itself while reassigning it:
128 <div class=
"code"><pre>
129 #define BOOST_PP_VALUE
20
130 #include BOOST_PP_ASSIGN_SLOT(
1)
132 #define BOOST_PP_VALUE
2 * BOOST_PP_SLOT(
1)
133 #include BOOST_PP_ASSIGN_SLOT(
1)
135 BOOST_PP_SLOT(
1) //
40
139 <li><a href=
"../ref/assign_slot.html">BOOST_PP_ASSIGN_SLOT
</a></li>
140 <li><a href=
"../ref/limit_slot_count.html">BOOST_PP_LIMIT_SLOT_COUNT
</a></li>
141 <li><a href=
"../ref/slot.html">BOOST_PP_SLOT
</a></li>
142 <li><a href=
"../ref/value.html">BOOST_PP_VALUE
</a></li>
144 <div class=
"sig">- Paul Mensonides
</div>
146 <div style=
"margin-left: 0px;">
147 <i>© Copyright
<a href=
"http://www.housemarque.com" target=
"_top">Housemarque Oy
</a> 2002</i>
148 </br><i>© Copyright Paul Mensonides
2002</i>
150 <div style=
"margin-left: 0px;">
151 <p><small>Distributed under the Boost Software License, Version
1.0. (See
152 accompanying file
<a href=
"../../../../LICENSE_1_0.txt">LICENSE_1_0.txt
</a> or
154 "http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt
</a>)
</small></p>