1 | <html> |
---|
2 | <head> |
---|
3 | <!-- Generated by the Spirit (http://spirit.sf.net) QuickDoc --> |
---|
4 | <title>Operators</title> |
---|
5 | <link rel="stylesheet" href="theme/style.css" type="text/css"> |
---|
6 | <link rel="prev" href="functions.html"> |
---|
7 | <link rel="next" href="statements.html"> |
---|
8 | </head> |
---|
9 | <body> |
---|
10 | <table width="100%" height="48" border="0" background="theme/bkd2.gif" cellspacing="2"> |
---|
11 | <tr> |
---|
12 | <td width="10"> |
---|
13 | </td> |
---|
14 | <td width="85%"> |
---|
15 | <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Operators</b></font> |
---|
16 | </td> |
---|
17 | <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" align="right" border="0"></a></td> |
---|
18 | </tr> |
---|
19 | </table> |
---|
20 | <br> |
---|
21 | <table border="0"> |
---|
22 | <tr> |
---|
23 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
24 | <td width="30"><a href="functions.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
25 | <td width="20"><a href="statements.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
26 | </tr> |
---|
27 | </table> |
---|
28 | <a name="lazy_operators"></a><h2>Lazy operators</h2><p> |
---|
29 | This facility provides a mechanism for lazily evaluating operators. Syntactically, a lazy operator looks and feels like an ordinary C/C++ infix, prefix or postfix operator. The operator application looks the same. However, unlike ordinary operators, the actual operator execution is deferred. Samples:</p> |
---|
30 | <code><pre> |
---|
31 | <span class=identifier>arg1 </span><span class=special>+ </span><span class=identifier>arg2 |
---|
32 | </span><span class=number>1 </span><span class=special>+ </span><span class=identifier>arg1 </span><span class=special>* </span><span class=identifier>arg2 |
---|
33 | </span><span class=number>1 </span><span class=special>/ -</span><span class=identifier>arg1 |
---|
34 | </span><span class=identifier>arg1 </span><span class=special>< </span><span class=number>150 |
---|
35 | </span></pre></code> |
---|
36 | <p> |
---|
37 | We have seen the lazy operators in action (see sample2.cpp) above. Let's go back and examine it a little bit further:</p> |
---|
38 | <code><pre> |
---|
39 | <span class=identifier>find_if</span><span class=special>(</span><span class=identifier>c</span><span class=special>.</span><span class=identifier>begin</span><span class=special>(), </span><span class=identifier>c</span><span class=special>.</span><span class=identifier>end</span><span class=special>(), </span><span class=identifier>arg1 </span><span class=special>% </span><span class=number>2 </span><span class=special>== </span><span class=number>1</span><span class=special>) |
---|
40 | </span></pre></code> |
---|
41 | <p> |
---|
42 | Through operator overloading, the expression "arg1 % 2 == 1" actually generates a composite. This composite object is passed on to STL's find_if function. In the viewpoint of STL, the composite is simply a functor expecting a single argument, the container's element. For each element in the container 'c', the element is passed on as an argument (arg1) to the composite (functor). The composite (functor) checks if this is an odd value based on the expression "arg1 % 2 == 1" where arg1 is iteratively replaced by the container's element.</p> |
---|
43 | <p> |
---|
44 | A set of classes implement all the C++ free operators. Like lazy functions (see functions), lazy operators are not immediately executed when invoked. Instead, a composite (see composite) object is created and returned to the caller. Example:</p> |
---|
45 | <code><pre> |
---|
46 | <span class=special>(</span><span class=identifier>arg1 </span><span class=special>+ </span><span class=identifier>arg2</span><span class=special>) * </span><span class=identifier>arg3 |
---|
47 | </span></pre></code> |
---|
48 | <p> |
---|
49 | does nothing more than return a composite. A second function call will evaluate the actual operators. Example:</p> |
---|
50 | <code><pre> |
---|
51 | <span class=keyword>int </span><span class=identifier>i </span><span class=special>= </span><span class=number>4</span><span class=special>, </span><span class=identifier>j </span><span class=special>= </span><span class=number>5</span><span class=special>, </span><span class=identifier>k </span><span class=special>= </span><span class=number>6</span><span class=special>; |
---|
52 | </span><span class=identifier>cout </span><span class=special><< ((</span><span class=identifier>arg1 </span><span class=special>+ </span><span class=identifier>arg2</span><span class=special>) * </span><span class=identifier>arg3</span><span class=special>)(</span><span class=identifier>i</span><span class=special>, </span><span class=identifier>j</span><span class=special>, </span><span class=identifier>k</span><span class=special>); |
---|
53 | </span></pre></code> |
---|
54 | <p> |
---|
55 | will print out "54".</p> |
---|
56 | <p> |
---|
57 | Arbitrarily complex expressions can be lazily evaluated following three simple rules:</p> |
---|
58 | <ol><li>Lazy evaluated binary operators apply when *at least* one of the operands is an actor object (see actor, primitives and composite). Consequently, if one of the operands is not an actor object, it is implicitly converted, by default, to an object of type actor<value<T> > (where T is the original type of the operand).</li><li>Lazy evaluated unary operators apply only to operands which are actor objects.</li><li>The result of a lazy operator is a composite actor object that can in turn apply to rule 1.</li></ol><p> |
---|
59 | Example:</p> |
---|
60 | <code><pre> |
---|
61 | <span class=special>-(</span><span class=identifier>arg1 </span><span class=special>+ </span><span class=number>3 </span><span class=special>+ </span><span class=number>6</span><span class=special>) |
---|
62 | </span></pre></code> |
---|
63 | <ol><li>Following rule 1, lazy evaluation is triggered since arg1 is an instance of an actor<argument<N> > class (see primitives).</li><li>The immediate right operand <3> is implicitly converted to an actor<value<int> >. Still following rule 1.</li><li>The result of this "arg1 + 3" expression is a composite object, following rule 3.</li><li>Now since "arg1 + 3" is a composite, following rule 1 again, its right operand <6> is implicitly converted to an actor<value<int> >.</li><li>Continuing, the result of "arg1 + 3" ... "+ 6" is again another composite. Rule 3.</li><li>The expression "arg1 + 3 + 6" being a composite, is the operand of the unary operator -. Following rule 2, the result is an actor object.</li><li>Folowing rule 3, the whole thing "-(arg1 + 3 + 6)" is a composite.</li></ol><p> |
---|
64 | Lazy-operator application is highly contagious. In most cases, a single argN actor infects all its immediate neighbors within a group (first level or parenthesized expression).</p> |
---|
65 | <p> |
---|
66 | Take note that although at least one of the operands must be a valid actor class in order for lazy evaluation to take effect, if this is not the case and we still want to lazily evaluate an expression, we can use var(x), val(x) or const(x) to transform the operand into a valid action object (see primitives). Example:</p> |
---|
67 | <code><pre> |
---|
68 | <span class=identifier>val</span><span class=special>(</span><span class=number>1</span><span class=special>) << </span><span class=number>3</span><span class=special>; |
---|
69 | </span></pre></code> |
---|
70 | <p> |
---|
71 | Supported operators:</p> |
---|
72 | <p> |
---|
73 | Unary operators:</p> |
---|
74 | <code><pre> |
---|
75 | <span class=identifier>prefix</span><span class=special>: ~, !, -, +, ++, --, & (</span><span class=identifier>reference</span><span class=special>), * (</span><span class=identifier>dereference</span><span class=special>) |
---|
76 | </span><span class=identifier>postfix</span><span class=special>: ++, -- |
---|
77 | </span></pre></code> |
---|
78 | <p> |
---|
79 | Binary operators:</p> |
---|
80 | <code><pre> |
---|
81 | <span class=special>=, [], +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= |
---|
82 | +, -, *, /, %, &, |, ^, <<, >> |
---|
83 | ==, !=, <, >, <=, >= |
---|
84 | &&, || |
---|
85 | </span></pre></code> |
---|
86 | <table border="0"> |
---|
87 | <tr> |
---|
88 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
89 | <td width="30"><a href="functions.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
90 | <td width="20"><a href="statements.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
91 | </tr> |
---|
92 | </table> |
---|
93 | <br> |
---|
94 | <hr size="1"> |
---|
95 | <p class="copyright">Copyright © 2001-2002 Joel de Guzman<br> |
---|
96 | <br> |
---|
97 | <font size="2">Use, modification and distribution is subject to the Boost Software |
---|
98 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
99 | http://www.boost.org/LICENSE_1_0.txt) </font> </p> |
---|
100 | </body> |
---|
101 | </html> |
---|