1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 3.2//EN">
5 <meta name=
"generator" content=
6 "HTML Tidy for Windows (vers 1st August 2002), see www.w3.org">
7 <meta http-equiv=
"Content-Type" content=
"text/html; charset=ISO-8859-1">
9 <title>Header
<boost/operators.hpp
> Documentation
</title>
12 <body text=
"black" bgcolor=
"white" link=
"blue" vlink=
"purple" alink=
"red">
13 <h1><img src=
"../../boost.png" alt=
"boost.png (6897 bytes)" align=
14 "middle" width=
"277" height=
"86">Header
<cite><<a href=
15 "../../boost/operators.hpp">boost/operators.hpp
</a>></cite></h1>
17 <p>The header
<cite><<a href=
18 "../../boost/operators.hpp">boost/operators.hpp
</a>></cite> supplies
19 several sets of class templates (in namespace
<code>boost
</code>). These
20 templates define operators at namespace scope in terms of a minimal
21 number of fundamental operators provided by the class.
</p>
23 <h2><a name=
"contents">Contents
</a></h2>
26 <li><a href=
"#contents">Contents
</a></li>
29 <a href=
"#rationale">Rationale
</a>
32 <li><a href=
"#semantics">Summary of Template Semantics
</a></li>
34 <li><a href=
"#concepts_note">Use of
<i>concepts
</i></a></li>
39 <a href=
"#usage">Usage
</a>
43 <a href=
"#two_arg">Two-Argument Template Forms
</a>
46 <li><a href=
"#two_arg_gen">General Considerations
</a></li>
48 <li><a href=
"#mixed_arithmetics">Mixed arithmetics
</a></li>
52 <li><a href=
"#chaining">Base Class Chaining and Object
55 <li><a href=
"#explicit_instantiation">Separate, Explicit
56 Instantiation
</a></li>
58 <li><a href=
"#portability">Requirement Portability
</a></li>
62 <li><a href=
"#example">Example
</a></li>
65 <a href=
"#arithmetic">Arithmetic operators
</a>
69 <a href=
"#smpl_oprs">Simple Arithmetic Operators
</a>
72 <li><a href=
"#ordering">Ordering Note
</a></li>
74 <li><a href=
"#symmetry">Symmetry Note
</a></li>
78 <li><a href=
"#grpd_oprs">Grouped Arithmetic Operators
</a></li>
80 <li><a href=
"#ex_oprs">Example Templates
</a></li>
82 <li><a href=
"#a_demo">Arithmetic Operators Demonstration and Test
88 <a href=
"#deref">Dereference Operators and Iterator Helpers
</a>
91 <li><a href=
"#dereference">Dereference operators
</a></li>
93 <li><a href=
"#grpd_iter_oprs">Grouped Iterator Operators
</a></li>
96 <a href=
"#iterator">Iterator Helpers
</a>
99 <li><a href=
"#iterator_helpers_notes">Iterator Helper
104 <li><a href=
"#i_demo">Iterator Demonstration and Test
109 <li><a href=
"#contributors">Contributors
</a></li>
111 <li><a href=
"#old_lib_note">Note for Users of Older Versions
</a></li>
114 <h2><a name=
"rationale">Rationale
</a></h2>
116 <p>Overloaded operators for class types typically occur in groups. If you
117 can write
<code>x
+
y
</code>, you probably also want to be able
118 to write
<code>x += y
</code>. If you can write
<code>x
< y,
</code> you
119 also want
<code>x
> y, x
>= y,
</code> and
<code>x
<= y
</code>.
120 Moreover, unless your class has really surprising behavior, some of these
121 related operators can be defined in terms of others (e.g.
<code>x
>= y
122 is equivalent to !(x
< y)
</code>). Replicating this boilerplate for
123 multiple classes is both tedious and error-prone. The
<cite><a href=
124 "../../boost/operators.hpp">boost/operators.hpp
</a></cite> templates help
125 by generating operators for you at namespace scope based on other
126 operators you've defined in your class.
</p>
128 <p>If, for example, you declare a class like this:
</p>
133 : boost::operators
<MyInt
>
135 bool operator
<(const MyInt
& x) const;
136 bool operator==(const MyInt
& x) const;
137 MyInt
& operator+=(const MyInt
& x);
138 MyInt
& operator-=(const MyInt
& x);
139 MyInt
& operator*=(const MyInt
& x);
140 MyInt
& operator/=(const MyInt
& x);
141 MyInt
& operator%=(const MyInt
& x);
142 MyInt
& operator|=(const MyInt
& x);
143 MyInt
& operator
&=(const MyInt
& x);
144 MyInt
& operator^=(const MyInt
& x);
145 MyInt
& operator++();
146 MyInt
& operator--();
151 <p>then the
<code><a href=
"#operators1">operators
<></a></code>
152 template adds more than a dozen additional operators, such as
153 <code>operator
></code>,
<code><=
</code>,
<code>>=
</code>, and
154 (binary)
<code>+
</code>.
<a href=
"#two_arg">Two-argument forms
</a> of the
155 templates are also provided to allow interaction with other types.
</p>
157 <h3>Summary of Template
<a name=
"semantics">Semantics
</a></h3>
160 <li>Each operator template completes the concept(s) it describes by
161 defining overloaded operators for its target class.
</li>
163 <li>The name of an operator class template indicates the
<a href=
164 "#concepts_note">concept
</a> that its target class will model.
</li>
166 <li>Usually, the target class uses an instantation of the operator
167 class template as a base class. Some operator templates support an
<a
168 href=
"#explicit_instantiation">alternate method
</a>.
</li>
170 <li>The concept can be compound,
<i>i.e.
</i> it may represent a common
171 combination of other, simpler concepts.
</li>
173 <li>Most operator templates require their target class to support
174 operations related to the operators supplied by the template. In
175 accordance with widely accepted
<a href=
176 "http://www.gotw.ca/gotw/004.htm">coding style recommendations
</a>, the
177 target class is often required to supply the assignment counterpart
178 operator of the concept's
"main operator." For example, the
179 <code>addable
</code> template requires
<code>operator+=(T
180 const
&)
</code> and in turn supplies
<code>operator+(T const
&, T
181 const
&)
</code>.
</li>
184 <h3>Use of
<i><a name=
"concepts_note">concepts
</a></i></h3>
186 <p>The discussed concepts are not necessarily the standard library's
187 concepts (CopyConstructible,
<i>etc.
</i>), although some of them could
188 be; they are what we call
<i>concepts with a small 'c'
</i>. In
189 particular, they are different from the former ones in that they
<em>do
190 not
</em> describe precise semantics of the operators they require to be
191 defined, except the requirements that (a) the semantics of the operators
192 grouped in one concept should be consistent (
<i>e.g.
</i> effects of
193 evaluating of
<code>a += b
</code> and
194 <code>a
=
a
+
b
</code> expressions should be the
195 same), and (b) that the return types of the operators should follow
196 semantics of return types of corresponding operators for built-in types
197 (
<i>e.g.
</i> <code>operator
<</code> should return a type convertible
198 to
<code>bool
</code>, and
<code>T::operator-=
</code> should return type
199 convertible to
<code>T
</code>). Such
"loose" requirements make operators
200 library applicable to broader set of target classes from different
201 domains,
<i>i.e.
</i> eventually more useful.
</p>
203 <h2><a name=
"usage">Usage
</a></h2>
205 <h3><a name=
"two_arg">Two-Argument
</a> Template Forms
</h3>
207 <h4><a name=
"two_arg_gen">General Considerations
</a></h4>
209 <p>The arguments to a binary operator commonly have identical types, but
210 it is not unusual to want to define operators which combine different
211 types. For
<a href=
"#example">example
</a>, one might want to multiply a
212 mathematical vector by a scalar. The two-argument template forms of the
213 arithmetic operator templates are supplied for this purpose. When
214 applying the two-argument form of a template, the desired return type of
215 the operators typically determines which of the two types in question
216 should be derived from the operator template. For example, if the result
217 of
<code>T
+
U
</code> is of type
<code>T
</code>, then
218 <code>T
</code> (not
<code>U
</code>) should be derived from
<code><a href=
219 "#addable2">addable
<T, U
></a></code>. The comparison templates
220 (
<code><a href=
"#less_than_comparable2">less_than_comparable
<T,
221 U
></a></code>,
<code><a href=
222 "#equality_comparable2">equality_comparable
<T, U
></a></code>,
223 <code><a href=
"#equivalent2">equivalent
<T, U
></a></code>, and
224 <code><a href=
"#partially_ordered2">partially_ordered
<T,
225 U
></a></code>) are exceptions to this guideline, since the return type
226 of the operators they define is
<code>bool
</code>.
</p>
228 <p>On compilers which do not support partial specialization, the
229 two-argument forms must be specified by using the names shown below with
230 the trailing
<code>'
2'
</code>. The single-argument forms with the
231 trailing
<code>'
1'
</code> are provided for symmetry and to enable certain
232 applications of the
<a href=
"#chaining">base class chaining
</a>
235 <h4><a name=
"mixed_arithmetics">Mixed Arithmetics
</a></h4>
237 <p>Another application of the two-argument template forms is for mixed
238 arithmetics between a type
<code>T
</code> and a type
<code>U
</code> that
239 is convertible to
<code>T
</code>. In this case there are two ways where
240 the two-argument template forms are helpful: one is to provide the
241 respective signatures for operator overloading, the second is
244 <p>With respect to the operator overloading assume
<i>e.g.
</i> that
245 <code>U
</code> is
<code>int
</code>, that
<code>T
</code> is an
246 user-defined unlimited integer type, and that
<code>double
247 operator-(double, const T
&)
</code> exists. If one wants to compute
248 <code>int - T
</code> and does not provide
<code>T operator-(int, const
249 T
&)
</code>, the compiler will consider
<code>double operator-(double,
250 const T
&)
</code> to be a better match than
<code>T operator-(const
251 T
&, const T
&)
</code>, which will probably be different from the
252 user's intention. To define a complete set of operator signatures,
253 additional 'left' forms of the two-argument template forms are provided
254 (
<code><a href=
"#subtractable2_left">subtractable2_left
<T,
255 U
></a></code>,
<code><a href=
"#dividable2_left">dividable2_left
<T,
256 U
></a></code>,
<code><a href=
"#modable2_left">modable2_left
<T,
257 U
></a></code>) that define the signatures for non-commutative
258 operators where
<code>U
</code> appears on the left hand side
259 (
<code>operator-(const U
&, const T
&)
</code>,
260 <code>operator/(const U
&, const T
&)
</code>,
<code>operator%(const
261 U
&, const T
&)
</code>).
</p>
263 <p>With respect to the performance observe that when one uses the single
264 type binary operator for mixed type arithmetics, the type
<code>U
</code>
265 argument has to be converted to type
<code>T
</code>. In practice,
266 however, there are often more efficient implementations of, say
267 <code>T::operator-=(const U
&)
</code> that avoid unnecessary
268 conversions from
<code>U
</code> to
<code>T
</code>. The two-argument
269 template forms of the arithmetic operator create additional operator
270 interfaces that use these more efficient implementations. There is,
271 however, no performance gain in the 'left' forms: they still need a
272 conversion from
<code>U
</code> to
<code>T
</code> and have an
273 implementation equivalent to the code that would be automatically created
274 by the compiler if it considered the single type binary operator to be
277 <h3>Base Class
<a name=
"chaining">Chaining
</a> and Object Size
</h3>
279 <p>Every operator class template, except the
<a href=
280 "#ex_oprs">arithmetic examples
</a> and the
<a href=
"#iterator">iterator
281 helpers
</a>, has an additional, but optional, template type parameter
282 <code>B
</code>. This parameter will be a publicly-derived base class of
283 the instantiated template. This means it must be a class type. It can be
284 used to avoid the bloating of object sizes that is commonly associated
285 with multiple-inheritance from several empty base classes (see the
<a
286 href=
"#old_lib_note">note for users of older versions
</a> for more
287 details). To provide support for a group of operators, use the
288 <code>B
</code> parameter to chain operator templates into a single-base
289 class hierarchy, demostrated in the
<a href=
"#example">usage example
</a>.
290 The technique is also used by the composite operator templates to group
291 operator definitions. If a chain becomes too long for the compiler to
292 support, try replacing some of the operator templates with a single
293 grouped operator template that chains the old templates together; the
294 length limit only applies to the number of templates directly in the
295 chain, not those hidden in group templates.
</p>
297 <p><strong>Caveat:
</strong> to chain to a base class which is
298 <em>not
</em> a Boost operator template when using the
<a href=
299 "#two_arg">single-argument form
</a> of a Boost operator template, you
300 must specify the operator template with the trailing
<code>'
1'
</code> in
301 its name. Otherwise the library will assume you mean to define a binary
302 operation combining the class you intend to use as a base class and the
303 class you're deriving.
</p>
305 <h3>Separate,
<a name=
"explicit_instantiation">Explicit
306 Instantiation
</a></h3>
308 <p>On some compilers (
<i>e.g.
</i> Borland, GCC) even single-inheritance
309 seems to cause an increase in object size in some cases. If you are not
310 defining a class template, you may get better object-size performance by
311 avoiding derivation altogether, and instead explicitly instantiating the
312 operator template as follows:
</p>
316 class myclass // lose the inheritance...
321 // explicitly instantiate the operators I need.
322 template struct less_than_comparable
<myclass
>;
323 template struct equality_comparable
<myclass
>;
324 template struct incrementable
<myclass
>;
325 template struct decrementable
<myclass
>;
326 template struct addable
<myclass,long
>;
327 template struct subtractable
<myclass,long
>;
331 <p>Note that some operator templates cannot use this workaround and must
332 be a base class of their primary operand type. Those templates define
333 operators which must be member functions, and the workaround needs the
334 operators to be independent friend functions. The relevant templates
339 "#dereferenceable">dereferenceable
<></a></code></li>
341 <li><code><a href=
"#indexable">indexable
<></a></code></li>
343 <li>Any composite operator template that includes at least one of the
347 <p>As Daniel Kr
ügler pointed out, this technique violates
14.6.5/
2
348 and is thus non-portable. The reasoning is, that the operators injected
349 by the instantiation of e.g.
350 <code>less_than_comparable
<myclass
></code> can not be found
351 by ADL according to the rules given by
3.4.2/
2, since myclass is
352 not an associated class of
353 <code>less_than_comparable
<myclass
></code>.
354 Thus only use this technique if all else fails.
</p>
356 <h3>Requirement
<a name=
"portability">Portability
</a></h3>
358 <p>Many compilers (
<i>e.g.
</i> MSVC
6.3, GCC
2.95.2) will not enforce the
359 requirements in the operator template tables unless the operations which
360 depend on them are actually used. This is not standard-conforming
361 behavior. In particular, although it would be convenient to derive all
362 your classes which need binary operators from the
<code><a href=
363 "#operators1">operators
<></a></code> and
<code><a href=
364 "#operators2">operators2
<></a></code> templates, regardless of
365 whether they implement all the requirements of those templates, this
366 shortcut is not portable. Even if this currently works with your
367 compiler, it may not work later.
</p>
369 <h2><a name=
"example">Example
</a></h2>
371 <p>This example shows how some of the
<a href=
"#arithmetic">arithmetic
372 operator templates
</a> can be used with a geometric point class
375 template
<class T
>
376 class point // note: private inheritance is OK here!
377 : boost::addable
< point
<T
> // point + point
378 , boost::subtractable
< point
<T
> // point - point
379 , boost::dividable2
< point
<T
>, T // point / T
380 , boost::multipliable2
< point
<T
>, T // point * T, T * point
388 point operator+=(const point
&);
389 // point operator+(point, const point
&) automatically
390 // generated by addable.
392 point operator-=(const point
&);
393 // point operator-(point, const point
&) automatically
394 // generated by subtractable.
397 // point operator*(point, const T
&) and
398 // point operator*(const T
&, point) auto-generated
402 // point operator/(point, const T
&) auto-generated
409 // now use the point
<> class:
411 template
<class T
>
412 T length(const point
<T
> p)
414 return sqrt(p.x()*p.x() + p.y()*p.y());
417 const point
<float
> right(
0,
1);
418 const point
<float
> up(
1,
0);
419 const point
<float
> pi_over_4 = up + right;
420 const point
<float
> pi_over_4_normalized = pi_over_4 / length(pi_over_4);
423 <h2><a name=
"arithmetic">Arithmetic
</a> Operators
</h2>
425 <p>The arithmetic operator templates ease the task of creating a custom
426 numeric type. Given a core set of operators, the templates add related
427 operators to the numeric class. These operations are like the ones the
428 standard arithmetic types have, and may include comparisons, adding,
429 incrementing, logical and bitwise manipulations,
<i>etc
</i>. Further,
430 since most numeric types need more than one of these operators, some
431 templates are provided to combine several of the basic operator templates
432 in one declaration.
</p>
434 <p>The requirements for the types used to instantiate the simple operator
435 templates are specified in terms of expressions which must be valid and
436 the expression's return type. The composite operator templates only list
437 what other templates they use. The supplied operations and requirements
438 of the composite operator templates can be inferred from the operations
439 and requirements of the listed components.
</p>
441 <h3><a name=
"smpl_oprs">Simple Arithmetic Operators
</a></h3>
443 <p>These templates are
"simple" since they provide operators based on a
444 single operation the base type has to provide. They have an additional
445 optional template parameter
<code>B
</code>, which is not shown, for the
446 <a href=
"#chaining">base class chaining
</a> technique.
</p>
448 <p>The primary operand type
<code>T
</code> needs to be of class type,
449 built-in types are not supported.
</p>
451 <table cellpadding=
"5" border=
"1" align=
"center">
453 Simple Arithmetic Operator Template Classes
458 <table align=
"center" border=
"1">
464 <td><code>T
</code>: primary operand type
</td>
466 <td><code>U
</code>: alternate operand type
</td>
470 <td><code>t
</code>,
<code>t1
</code>: values of type
473 <td><code>u
</code>: value of type
<code>U
</code></td>
482 <th>Supplied Operations
</th>
484 <th>Requirements
</th>
486 <th>Propagates
<code>constexpr
</code>?
</th>
491 "less_than_comparable1">less_than_comparable
<T
></a></code><br>
492 <code>less_than_comparable1
<T
></code></td>
494 <td><code>bool operator
>(const T
&, const T
&)
</code><br>
495 <code>bool operator
<=(const T
&, const T
&)
</code><br>
496 <code>bool operator
>=(const T
&, const T
&)
</code></td>
498 <td><code>t
< t1
</code>.
<br>
499 Return convertible to
<code>bool
</code>. See the
<a href=
500 "#ordering">Ordering Note
</a>.
</td>
502 <td>Since
<code>C++
11</code><br>
503 <span style=
"font-size:small;">(except
<a href=
"https://developercommunity.visualstudio.com/content/problem/414193/rejects-valid-constexpr-marked-friend-function-def.html">MSVC
< v19.22
</a>)
</span></td>
507 <td><code><a name=
"less_than_comparable2">less_than_comparable
<T,
509 <code>less_than_comparable2
<T, U
></code></td>
511 <td><code>bool operator
<=(const T
&, const U
&)
</code><br>
512 <code>bool operator
>=(const T
&, const U
&)
</code><br>
513 <code>bool operator
>(const U
&, const T
&)
</code><br>
514 <code>bool operator
<(const U
&, const T
&)
</code><br>
515 <code>bool operator
<=(const U
&, const T
&)
</code><br>
516 <code>bool operator
>=(const U
&, const T
&)
</code></td>
518 <td><code>t
< u
</code>.
<code>t
> u
</code>.
<br>
519 Returns convertible to
<code>bool
</code>. See the
<a href=
520 "#ordering">Ordering Note
</a>.
</td>
522 <td>Since
<code>C++
11</code><br>
523 <span style=
"font-size:small;">(except
<a href=
"https://developercommunity.visualstudio.com/content/problem/414193/rejects-valid-constexpr-marked-friend-function-def.html">MSVC
< v19.22
</a>)
</span></td>
528 "equality_comparable1">equality_comparable
<T
></a></code><br>
529 <code>equality_comparable1
<T
></code></td>
531 <td><code>bool operator!=(const T
&, const T
&)
</code></td>
533 <td><code>t == t1
</code>.
<br>
534 Return convertible to
<code>bool
</code>.
</td>
536 <td>Since
<code>C++
11</code><br>
537 <span style=
"font-size:small;">(except
<a href=
"https://developercommunity.visualstudio.com/content/problem/414193/rejects-valid-constexpr-marked-friend-function-def.html">MSVC
< v19.22
</a>)
</span></td>
541 <td><code><a name=
"equality_comparable2">equality_comparable
<T,
543 <code>equality_comparable2
<T, U
></code></td>
545 <td><code>bool operator==(const U
&, const T
&)
</code><br>
546 <code>bool operator!=(const U
&, const T
&)
</code><br>
547 <code>bool operator!=(const T
&, const U
&)
</code></td>
549 <td><code>t == u
</code>.
<br>
550 Return convertible to
<code>bool
</code>.
</td>
552 <td>Since
<code>C++
11</code><br>
553 <span style=
"font-size:small;">(except
<a href=
"https://developercommunity.visualstudio.com/content/problem/414193/rejects-valid-constexpr-marked-friend-function-def.html">MSVC
< v19.22
</a>)
</span></td>
557 <td><code><a name=
"addable1">addable
<T
></a></code><br>
558 <code>addable1
<T
></code></td>
560 <td><code>T operator+(const T
&, const T
&)
</code></td>
562 <td><code>T temp(t); temp += t1
</code>.
<br>
563 Return convertible to
<code>T
</code>. See the
<a href=
564 "#symmetry">Symmetry Note
</a>.
</td>
570 <td><code><a name=
"addable2">addable
<T, U
></a></code><br>
571 <code>addable2
<T, U
></code></td>
573 <td><code>T operator+(const T
&, const U
&)
</code><br>
574 <code>T operator+(const U
&, const T
& )
</code></td>
576 <td><code>T temp(t); temp += u
</code>.
<br>
577 Return convertible to
<code>T
</code>. See the
<a href=
578 "#symmetry">Symmetry Note
</a>.
</td>
585 "subtractable1">subtractable
<T
></a></code><br>
586 <code>subtractable1
<T
></code></td>
588 <td><code>T operator-(const T
&, const T
&)
</code></td>
590 <td><code>T temp(t); temp -= t1
</code>.
<br>
591 Return convertible to
<code>T
</code>. See the
<a href=
592 "#symmetry">Symmetry Note
</a>.
</td>
598 <td><code><a name=
"subtractable2">subtractable
<T,
600 <code>subtractable2
<T, U
></code></td>
602 <td><code>T operator-(const T
&, const U
&)
</code></td>
604 <td><code>T temp(t); temp -= u
</code>.
<br>
605 Return convertible to
<code>T
</code>. See the
<a href=
606 "#symmetry">Symmetry Note
</a>.
</td>
612 <td><code><a name=
"subtractable2_left">subtractable2_left
<T,
613 U
></a></code></td>
615 <td><code>T operator-(const U
&, const T
&)
</code></td>
617 <td><code>T temp(u); temp -= t
</code>.
<br>
618 Return convertible to
<code>T
</code>.
</td>
625 "multipliable1">multipliable
<T
></a></code><br>
626 <code>multipliable1
<T
></code></td>
628 <td><code>T operator*(const T
&, const T
&)
</code></td>
630 <td><code>T temp(t); temp *= t1
</code>.
<br>
631 Return convertible to
<code>T
</code>. See the
<a href=
632 "#symmetry">Symmetry Note
</a>.
</td>
638 <td><code><a name=
"multipliable2">multipliable
<T,
640 <code>multipliable2
<T, U
></code></td>
642 <td><code>T operator*(const T
&, const U
&)
</code><br>
643 <code>T operator*(const U
&, const T
&)
</code></td>
645 <td><code>T temp(t); temp *= u
</code>.
<br>
646 Return convertible to
<code>T
</code>. See the
<a href=
647 "#symmetry">Symmetry Note
</a>.
</td>
653 <td><code><a name=
"dividable1">dividable
<T
></a></code><br>
654 <code>dividable1
<T
></code></td>
656 <td><code>T operator/(const T
&, const T
&)
</code></td>
658 <td><code>T temp(t); temp /= t1
</code>.
<br>
659 Return convertible to
<code>T
</code>. See the
<a href=
660 "#symmetry">Symmetry Note
</a>.
</td>
666 <td><code><a name=
"dividable2">dividable
<T, U
></a></code><br>
667 <code>dividable2
<T, U
></code></td>
669 <td><code>T operator/(const T
&, const U
&)
</code></td>
671 <td><code>T temp(t); temp /= u
</code>.
<br>
672 Return convertible to
<code>T
</code>. See the
<a href=
673 "#symmetry">Symmetry Note
</a>.
</td>
679 <td><code><a name=
"dividable2_left">dividable2_left
<T,
680 U
></a></code></td>
682 <td><code>T operator/(const U
&, const T
&)
</code></td>
684 <td><code>T temp(u); temp /= t
</code>.
<br>
685 Return convertible to
<code>T
</code>.
</td>
691 <td><code><a name=
"modable1">modable
<T
></a></code><br>
692 <code>modable1
<T
></code></td>
694 <td><code>T operator%(const T
&, const T
&)
</code></td>
696 <td><code>T temp(t); temp %= t1
</code>.
<br>
697 Return convertible to
<code>T
</code>. See the
<a href=
698 "#symmetry">Symmetry Note
</a>.
</td>
704 <td><code><a name=
"modable2">modable
<T, U
></a></code><br>
705 <code>modable2
<T, U
></code></td>
707 <td><code>T operator%(const T
&, const U
&)
</code></td>
709 <td><code>T temp(t); temp %= u
</code>.
<br>
710 Return convertible to
<code>T
</code>. See the
<a href=
711 "#symmetry">Symmetry Note
</a>.
</td>
717 <td><code><a name=
"modable2_left">modable2_left
<T,
718 U
></a></code></td>
720 <td><code>T operator%(const U
&, const T
&)
</code></td>
722 <td><code>T temp(u); temp %= t
</code>.
<br>
723 Return convertible to
<code>T
</code>.
</td>
729 <td><code><a name=
"orable1">orable
<T
></a></code><br>
730 <code>orable1
<T
></code></td>
732 <td><code>T operator|(const T
&, const T
&)
</code></td>
734 <td><code>T temp(t); temp |= t1
</code>.
<br>
735 Return convertible to
<code>T
</code>. See the
<a href=
736 "#symmetry">Symmetry Note
</a>.
</td>
742 <td><code><a name=
"orable2">orable
<T, U
></a></code><br>
743 <code>orable2
<T, U
></code></td>
745 <td><code>T operator|(const T
&, const U
&)
</code><br>
746 <code>T operator|(const U
&, const T
&)
</code></td>
748 <td><code>T temp(t); temp |= u
</code>.
<br>
749 Return convertible to
<code>T
</code>. See the
<a href=
750 "#symmetry">Symmetry Note
</a>.
</td>
756 <td><code><a name=
"andable1">andable
<T
></a></code><br>
757 <code>andable1
<T
></code></td>
759 <td><code>T operator
&(const T
&, const T
&)
</code></td>
761 <td><code>T temp(t); temp
&= t1
</code>.
<br>
762 Return convertible to
<code>T
</code>. See the
<a href=
763 "#symmetry">Symmetry Note
</a>.
</td>
769 <td><code><a name=
"andable2">andable
<T, U
></a></code><br>
770 <code>andable2
<T, U
></code></td>
772 <td><code>T operator
&(const T
&, const U
&)
</code><br>
773 <code>T operator
&(const U
&, const T
&)
</code></td>
775 <td><code>T temp(t); temp
&= u
</code>.
<br>
776 Return convertible to
<code>T
</code>. See the
<a href=
777 "#symmetry">Symmetry Note
</a>.
</td>
783 <td><code><a name=
"xorable1">xorable
<T
></a></code><br>
784 <code>xorable1
<T
></code></td>
786 <td><code>T operator^(const T
&, const T
&)
</code></td>
788 <td><code>T temp(t); temp ^= t1
</code>.
<br>
789 Return convertible to
<code>T
</code>. See the
<a href=
790 "#symmetry">Symmetry Note
</a>.
</td>
796 <td><code><a name=
"xorable2">xorable
<T, U
></a></code><br>
797 <code>xorable2
<T, U
></code></td>
799 <td><code>T operator^(const T
&, const U
&)
</code><br>
800 <code>T operator^(const U
&, const T
&)
</code></td>
802 <td><code>T temp(t); temp ^= u
</code>.
<br>
803 Return convertible to
<code>T
</code>. See the
<a href=
804 "#symmetry">Symmetry Note
</a>.
</td>
811 "incrementable">incrementable
<T
></a></code></td>
813 <td><code>T operator++(T
&, int)
</code></td>
815 <td><code>T temp(t); ++t
</code><br>
816 Return convertible to
<code>T
</code>.
</td>
823 "decrementable">decrementable
<T
></a></code></td>
825 <td><code>T operator--(T
&, int)
</code></td>
827 <td><code>T temp(t); --t;
</code><br>
828 Return convertible to
<code>T
</code>.
</td>
835 "left_shiftable1">left_shiftable
<T
></a></code><br>
836 <code>left_shiftable1
<T
></code></td>
838 <td><code>T operator
<<(const T
&, const T
&)
</code></td>
840 <td><code>T temp(t); temp
<<= t1
</code>.
<br>
841 Return convertible to
<code>T
</code>. See the
<a href=
842 "#symmetry">Symmetry Note
</a>.
</td>
848 <td><code><a name=
"left_shiftable2">left_shiftable
<T,
850 <code>left_shiftable2
<T, U
></code></td>
852 <td><code>T operator
<<(const T
&, const U
&)
</code></td>
854 <td><code>T temp(t); temp
<<= u
</code>.
<br>
855 Return convertible to
<code>T
</code>. See the
<a href=
856 "#symmetry">Symmetry Note
</a>.
</td>
863 "right_shiftable1">right_shiftable
<T
></a></code><br>
864 <code>right_shiftable1
<T
></code></td>
866 <td><code>T operator
>>(const T
&, const T
&)
</code></td>
868 <td><code>T temp(t); temp
>>= t1
</code>.
<br>
869 Return convertible to
<code>T
</code>. See the
<a href=
870 "#symmetry">Symmetry Note
</a>.
</td>
876 <td><code><a name=
"right_shiftable2">right_shiftable
<T,
878 <code>right_shiftable2
<T, U
></code></td>
880 <td><code>T operator
>>(const T
&, const U
&)
</code></td>
882 <td><code>T temp(t); temp
>>= u
</code>.
<br>
883 Return convertible to
<code>T
</code>. See the
<a href=
884 "#symmetry">Symmetry Note
</a>.
</td>
890 <td><code><a name=
"equivalent1">equivalent
<T
></a></code><br>
891 <code>equivalent1
<T
></code></td>
893 <td><code>bool operator==(const T
&, const T
&)
</code></td>
895 <td><code>t
< t1
</code>.
<br>
896 Return convertible to
<code>bool
</code>. See the
<a href=
897 "#ordering">Ordering Note
</a>.
</td>
899 <td>Since
<code>C++
11</code><br>
900 <span style=
"font-size:small;">(except
<a href=
"https://developercommunity.visualstudio.com/content/problem/414193/rejects-valid-constexpr-marked-friend-function-def.html">MSVC
< v19.22
</a>)
</span></td>
904 <td><code><a name=
"equivalent2">equivalent
<T, U
></a></code><br>
905 <code>equivalent2
<T, U
></code></td>
907 <td><code>bool operator==(const T
&, const U
&)
</code></td>
909 <td><code>t
< u
</code>.
<code>t
> u
</code>.
<br>
910 Returns convertible to
<code>bool
</code>. See the
<a href=
911 "#ordering">Ordering Note
</a>.
</td>
913 <td>Since
<code>C++
11</code><br>
914 <span style=
"font-size:small;">(except
<a href=
"https://developercommunity.visualstudio.com/content/problem/414193/rejects-valid-constexpr-marked-friend-function-def.html">MSVC
< v19.22
</a>)
</span></td>
919 "partially_ordered1">partially_ordered
<T
></a></code><br>
920 <code>partially_ordered1
<T
></code></td>
922 <td><code>bool operator
>(const T
&, const T
&)
</code><br>
923 <code>bool operator
<=(const T
&, const T
&)
</code><br>
924 <code>bool operator
>=(const T
&, const T
&)
</code></td>
926 <td><code>t
< t1
</code>.
<code>t == t1
</code>.
<br>
927 Returns convertible to
<code>bool
</code>. See the
<a href=
928 "#ordering">Ordering Note
</a>.
</td>
930 <td>Since
<code>C++
11</code><br>
931 <span style=
"font-size:small;">(except
<a href=
"https://developercommunity.visualstudio.com/content/problem/414193/rejects-valid-constexpr-marked-friend-function-def.html">MSVC
< v19.22
</a>)
</span></td>
935 <td><code><a name=
"partially_ordered2">partially_ordered
<T,
937 <code>partially_ordered2
<T, U
></code></td>
939 <td><code>bool operator
<=(const T
&, const U
&)
</code><br>
940 <code>bool operator
>=(const T
&, const U
&)
</code><br>
941 <code>bool operator
>(const U
&, const T
&)
</code><br>
942 <code>bool operator
<(const U
&, const T
&)
</code><br>
943 <code>bool operator
<=(const U
&, const T
&)
</code><br>
944 <code>bool operator
>=(const U
&, const T
&)
</code></td>
946 <td><code>t
< u
</code>.
<code>t
> u
</code>.
<code>t ==
948 Returns convertible to
<code>bool
</code>. See the
<a href=
949 "#ordering">Ordering Note
</a>.
</td>
951 <td>Since
<code>C++
11</code><br>
952 <span style=
"font-size:small;">(except
<a href=
"https://developercommunity.visualstudio.com/content/problem/414193/rejects-valid-constexpr-marked-friend-function-def.html">MSVC
< v19.22
</a>)
</span></td>
956 <h4><a name=
"ordering">Ordering
</a> Note
</h4>
958 <p>The
<code><a href=
959 "#less_than_comparable1">less_than_comparable
<T
></a></code> and
960 <code><a href=
"#partially_ordered1">partially_ordered
<T
></a></code>
961 templates provide the same set of operations. However, the workings of
963 "#less_than_comparable1">less_than_comparable
<T
></a></code> assume
964 that all values of type
<code>T
</code> can be placed in a total order. If
965 that is not true (
<i>e.g.
</i> Not-a-Number values in IEEE floating point
966 arithmetic), then
<code><a href=
967 "#partially_ordered1">partially_ordered
<T
></a></code> should be
968 used. The
<code><a href=
969 "#partially_ordered1">partially_ordered
<T
></a></code> template can
970 be used for a totally-ordered type, but it is not as efficient as
972 "#less_than_comparable1">less_than_comparable
<T
></a></code>. This
973 rule also applies for
<code><a href=
974 "#less_than_comparable2">less_than_comparable
<T, U
></a></code> and
975 <code><a href=
"#partially_ordered2">partially_ordered
<T,
976 U
></a></code> with respect to the ordering of all
<code>T
</code> and
977 <code>U
</code> values, and for both versions of
<code><a href=
978 "#equivalent1">equivalent
<></a></code>. The solution for
<code><a
979 href=
"#equivalent1">equivalent
<></a></code> is to write a custom
980 <code>operator==
</code> for the target class.
</p>
982 <h4><a name=
"symmetry">Symmetry
</a> Note
</h4>
984 <p>Before talking about symmetry, we need to talk about optimizations to
985 understand the reasons for the different implementation styles of
986 operators. Let's have a look at
<code>operator+
</code> for a class
987 <code>T
</code> as an example:
</p>
989 T operator+( const T
& lhs, const T
& rhs )
991 return T( lhs ) += rhs;
994 This would be a normal implementation of
<code>operator+
</code>, but it
995 is not an efficient one. An unnamed local copy of
<code>lhs
</code> is
996 created,
<code>operator+=
</code> is called on it and it is copied to the
997 function return value (which is another unnamed object of type
998 <code>T
</code>). The standard doesn't generally allow the intermediate
999 object to be optimized away:
1002 3.7.2/
2: Automatic storage duration
<br>
1004 If a named automatic object has initialization or a destructor with
1005 side effects, it shall not be destroyed before the end of its block,
1006 nor shall it be eliminated as an optimization even if it appears to be
1007 unused, except that a class object or its copy may be eliminated as
1010 The reference to
12.8 is important for us:
1013 12.8/
15: Copying class objects
<br>
1015 For a function with a class return type, if the expression in the
1016 return statement is the name of a local object, and the cv-unqualified
1017 type of the local object is the same as the function return type, an
1018 implementation is permitted to omit creating the temporary object to
1019 hold the function return value, even if the class copy constructor or
1020 destructor has side effects.
1022 This optimization is known as the named return value optimization (NRVO),
1023 which leads us to the following implementation for
1024 <code>operator+
</code>:
1026 T operator+( const T
& lhs, const T
& rhs )
1033 Given this implementation, the compiler is allowed to remove the
1034 intermediate object. Sadly, not all compiler implement the NRVO, some
1035 even implement it in an incorrect way which makes it useless here.
1036 Without the NRVO, the NRVO-friendly code is no worse than the original
1037 code showed above, but there is another possible implementation, which
1038 has some very special properties:
1040 T operator+( T lhs, const T
& rhs )
1045 The difference to the first implementation is that
<code>lhs
</code> is
1046 not taken as a constant reference used to create a copy; instead,
1047 <code>lhs
</code> is a by-value parameter, thus it is already the copy
1048 needed. This allows another optimization (
12.2/
2) for some cases.
1049 Consider
<code>a
+
b
+
c
</code> where the result of
1050 <code>a
+
b
</code> is not copied when used as
<code>lhs
</code>
1051 when adding
<code>c
</code>. This is more efficient than the original
1052 code, but not as efficient as a compiler using the NRVO. For most people,
1053 it is still preferable for compilers that don't implement the NRVO, but
1054 the
<code>operator+
</code> now has a different function signature. Also,
1055 the number of objects created differs for
1056 <code>(a
+
b
)
+
c
</code> and
1057 <code>a
+
(
b
+
c
)
</code>. Most probably,
1058 this won't be a problem for you, but if your code relies on the function
1059 signature or a strict symmetric behaviour, you should set
1060 <code>BOOST_FORCE_SYMMETRIC_OPERATORS
</code> in your user-config. This
1061 will force the NRVO-friendly implementation to be used even for compilers
1062 that don't implement the NRVO.
<br>
1065 <h3><a name=
"grpd_oprs">Grouped Arithmetic Operators
</a></h3>
1067 <p>The following templates provide common groups of related operations.
1068 For example, since a type which is addable is usually also subractable,
1069 the
<code><a href=
"#additive1">additive
</a></code> template provides the
1070 combined operators of both. The grouped operator templates have an
1071 additional optional template parameter
<code>B
</code>, which is not
1072 shown, for the
<a href=
"#chaining">base class chaining
</a> technique.
</p>
1074 <table cellpadding=
"5" border=
"1" align=
"center">
1076 Grouped Arithmetic Operator Template Classes
1081 <table align=
"center" border=
"1">
1087 <td><code>T
</code>: primary operand type
</td>
1089 <td><code>U
</code>: alternate operand type
</td>
1098 <th>Component Operator Templates
</th>
1103 "totally_ordered1">totally_ordered
<T
></a></code><br>
1104 <code>totally_ordered1
<T
></code></td>
1109 "#less_than_comparable1">less_than_comparable
<T
></a></code></li>
1112 "#equality_comparable1">equality_comparable
<T
></a></code></li>
1118 <td><code><a name=
"totally_ordered2">totally_ordered
<T,
1119 U
></a></code><br>
1120 <code>totally_ordered2
<T, U
></code></td>
1125 "#less_than_comparable2">less_than_comparable
<T,
1126 U
></a></code></li>
1129 "#equality_comparable2">equality_comparable
<T,
1130 U
></a></code></li>
1136 <td><code><a name=
"additive1">additive
<T
></a></code><br>
1137 <code>additive1
<T
></code></td>
1141 <li><code><a href=
"#addable1">addable
<T
></a></code></li>
1144 "#subtractable1">subtractable
<T
></a></code></li>
1150 <td><code><a name=
"additive2">additive
<T, U
></a></code><br>
1151 <code>additive2
<T, U
></code></td>
1155 <li><code><a href=
"#addable2">addable
<T, U
></a></code></li>
1157 <li><code><a href=
"#subtractable2">subtractable
<T,
1158 U
></a></code></li>
1165 "multiplicative1">multiplicative
<T
></a></code><br>
1166 <code>multiplicative1
<T
></code></td>
1171 "#multipliable1">multipliable
<T
></a></code></li>
1174 "#dividable1">dividable
<T
></a></code></li>
1180 <td><code><a name=
"multiplicative2">multiplicative
<T,
1181 U
></a></code><br>
1182 <code>multiplicative2
<T, U
></code></td>
1186 <li><code><a href=
"#multipliable2">multipliable
<T,
1187 U
></a></code></li>
1189 <li><code><a href=
"#dividable2">dividable
<T,
1190 U
></a></code></li>
1197 "integer_multiplicative1">integer_multiplicative
<T
></a></code><br>
1199 <code>integer_multiplicative1
<T
></code></td>
1204 "#multiplicative1">multiplicative
<T
></a></code></li>
1206 <li><code><a href=
"#modable1">modable
<T
></a></code></li>
1213 "integer_multiplicative2">integer_multiplicative
<T,
1214 U
></a></code><br>
1215 <code>integer_multiplicative2
<T, U
></code></td>
1219 <li><code><a href=
"#multiplicative2">multiplicative
<T,
1220 U
></a></code></li>
1222 <li><code><a href=
"#modable2">modable
<T, U
></a></code></li>
1228 <td><code><a name=
"arithmetic1">arithmetic
<T
></a></code><br>
1229 <code>arithmetic1
<T
></code></td>
1233 <li><code><a href=
"#additive1">additive
<T
></a></code></li>
1236 "#multiplicative1">multiplicative
<T
></a></code></li>
1242 <td><code><a name=
"arithmetic2">arithmetic
<T, U
></a></code><br>
1243 <code>arithmetic2
<T, U
></code></td>
1247 <li><code><a href=
"#additive2">additive
<T,
1248 U
></a></code></li>
1250 <li><code><a href=
"#multiplicative2">multiplicative
<T,
1251 U
></a></code></li>
1258 "integer_arithmetic1">integer_arithmetic
<T
></a></code><br>
1259 <code>integer_arithmetic1
<T
></code></td>
1263 <li><code><a href=
"#additive1">additive
<T
></a></code></li>
1266 "#integer_multiplicative1">integer_multiplicative
<T
></a></code></li>
1272 <td><code><a name=
"integer_arithmetic2">integer_arithmetic
<T,
1273 U
></a></code><br>
1274 <code>integer_arithmetic2
<T, U
></code></td>
1278 <li><code><a href=
"#additive2">additive
<T,
1279 U
></a></code></li>
1282 "#integer_multiplicative2">integer_multiplicative
<T,
1283 U
></a></code></li>
1289 <td><code><a name=
"bitwise1">bitwise
<T
></a></code><br>
1290 <code>bitwise1
<T
></code></td>
1294 <li><code><a href=
"#xorable1">xorable
<T
></a></code></li>
1296 <li><code><a href=
"#andable1">andable
<T
></a></code></li>
1298 <li><code><a href=
"#orable1">orable
<T
></a></code></li>
1304 <td><code><a name=
"bitwise2">bitwise
<T, U
></a></code><br>
1305 <code>bitwise2
<T, U
></code></td>
1309 <li><code><a href=
"#xorable2">xorable
<T, U
></a></code></li>
1311 <li><code><a href=
"#andable2">andable
<T, U
></a></code></li>
1313 <li><code><a href=
"#orable2">orable
<T, U
></a></code></li>
1320 "unit_steppable">unit_steppable
<T
></a></code></td>
1325 "#incrementable">incrementable
<T
></a></code></li>
1328 "#decrementable">decrementable
<T
></a></code></li>
1334 <td><code><a name=
"shiftable1">shiftable
<T
></a></code><br>
1335 <code>shiftable1
<T
></code></td>
1340 "#left_shiftable1">left_shiftable
<T
></a></code></li>
1343 "#right_shiftable1">right_shiftable
<T
></a></code></li>
1349 <td><code><a name=
"shiftable2">shiftable
<T, U
></a></code><br>
1350 <code>shiftable2
<T, U
></code></td>
1354 <li><code><a href=
"#left_shiftable2">left_shiftable
<T,
1355 U
></a></code></li>
1357 <li><code><a href=
"#right_shiftable2">right_shiftable
<T,
1358 U
></a></code></li>
1365 "ring_operators1">ring_operators
<T
></a></code><br>
1366 <code>ring_operators1
<T
></code></td>
1370 <li><code><a href=
"#additive1">additive
<T
></a></code></li>
1373 "#multipliable1">multipliable
<T
></a></code></li>
1379 <td><code><a name=
"ring_operators2">ring_operators
<T,
1380 U
></a></code><br>
1381 <code>ring_operators2
<T, U
></code></td>
1385 <li><code><a href=
"#additive2">additive
<T,
1386 U
></a></code></li>
1388 <li><code><a href=
"#subtractable2_left">subtractable2_left
<T,
1389 U
></a></code></li>
1391 <li><code><a href=
"#multipliable2">multipliable
<T,
1392 U
></a></code></li>
1399 "ordered_ring_operators1">ordered_ring_operators
<T
></a></code><br>
1401 <code>ordered_ring_operators1
<T
></code></td>
1406 "#ring_operators1">ring_operators
<T
></a></code></li>
1409 "#totally_ordered1">totally_ordered
<T
></a></code></li>
1416 "ordered_ring_operators2">ordered_ring_operators
<T,
1417 U
></a></code><br>
1418 <code>ordered_ring_operators2
<T, U
></code></td>
1422 <li><code><a href=
"#ring_operators2">ring_operators
<T,
1423 U
></a></code></li>
1425 <li><code><a href=
"#totally_ordered2">totally_ordered
<T,
1426 U
></a></code></li>
1433 "field_operators1">field_operators
<T
></a></code><br>
1434 <code>field_operators1
<T
></code></td>
1439 "#ring_operators1">ring_operators
<T
></a></code></li>
1442 "#dividable1">dividable
<T
></a></code></li>
1448 <td><code><a name=
"field_operators2">field_operators
<T,
1449 U
></a></code><br>
1450 <code>field_operators2
<T, U
></code></td>
1454 <li><code><a href=
"#ring_operators2">ring_operators
<T,
1455 U
></a></code></li>
1457 <li><code><a href=
"#dividable2">dividable
<T,
1458 U
></a></code></li>
1460 <li><code><a href=
"#dividable2_left">dividable2_left
<T,
1461 U
></a></code></li>
1468 "ordered_field_operators1">ordered_field_operators
<T
></a></code><br>
1470 <code>ordered_field_operators1
<T
></code></td>
1475 "#field_operators1">field_operators
<T
></a></code></li>
1478 "#totally_ordered1">totally_ordered
<T
></a></code></li>
1485 "ordered_field_operators2">ordered_field_operators
<T,
1486 U
></a></code><br>
1487 <code>ordered_field_operators2
<T, U
></code></td>
1491 <li><code><a href=
"#field_operators2">field_operators
<T,
1492 U
></a></code></li>
1494 <li><code><a href=
"#totally_ordered2">totally_ordered
<T,
1495 U
></a></code></li>
1502 "euclidean_ring_operators1">euclidean_ring_operators
<T
></a></code><br>
1504 <code>euclidean_ring_operators1
<T
></code></td>
1509 "#ring_operators1">ring_operators
<T
></a></code></li>
1512 "#dividable1">dividable
<T
></a></code></li>
1514 <li><code><a href=
"#modable1">modable
<T
></a></code></li>
1521 "euclidean_ring_operators2">euclidean_ring_operators
<T,
1522 U
></a></code><br>
1523 <code>euclidean_ring_operators2
<T, U
></code></td>
1527 <li><code><a href=
"#ring_operators2">ring_operators
<T,
1528 U
></a></code></li>
1530 <li><code><a href=
"#dividable2">dividable
<T,
1531 U
></a></code></li>
1533 <li><code><a href=
"#dividable2_left">dividable2_left
<T,
1534 U
></a></code></li>
1536 <li><code><a href=
"#modable2">modable
<T, U
></a></code></li>
1538 <li><code><a href=
"#modable2_left">modable2_left
<T,
1539 U
></a></code></li>
1546 "ordered_euclidean_ring_operators1">ordered_euclidean_ring_operators
<T
></a></code><br>
1548 <code>ordered_euclidean_ring_operators1
<T
></code></td>
1553 "#euclidean_ring_operators1">euclidean_ring_operators
<T
></a></code></li>
1556 "#totally_ordered1">totally_ordered
<T
></a></code></li>
1563 "ordered_euclidean_ring_operators2">ordered_euclidean_ring_operators
<T,
1564 U
></a></code><br>
1565 <code>ordered_euclidean_ring_operators2
<T, U
></code></td>
1570 "#euclidean_ring_operators2">euclidean_ring_operators
<T,
1571 U
></a></code></li>
1573 <li><code><a href=
"#totally_ordered2">totally_ordered
<T,
1574 U
></a></code></li>
1580 <h4>Spelling: euclidean vs. euclidian
</h4>
1582 <p>Older versions of the Boost.Operators library used
1583 "<code>euclidian
</code>", but it was pointed out that
1584 "<code>euclidean
</code>" is the more common spelling.
1585 To be compatible with older version, the library now supports
1589 <h3><a name=
"ex_oprs">Example
</a> Templates
</h3>
1591 <p>The arithmetic operator class templates
<code><a href=
1592 "#operators1">operators
<></a></code> and
<code><a href=
1593 "#operators2">operators2
<></a></code> are examples of
1594 non-extensible operator grouping classes. These legacy class templates,
1595 from previous versions of the header, cannot be used for
<a href=
1596 "#chaining">base class chaining
</a>.
</p>
1598 <table cellpadding=
"5" border=
"1" align=
"center">
1600 Final Arithmetic Operator Template Classes
1605 <table align=
"center" border=
"1">
1611 <td><code>T
</code>: primary operand type
</td>
1613 <td><code>U
</code>: alternate operand type
</td>
1622 <th>Component Operator Templates
</th>
1626 <td><code><a name=
"operators1">operators
<T
></a></code></td>
1631 "#totally_ordered1">totally_ordered
<T
></a></code></li>
1634 "#integer_arithmetic1">integer_arithmetic
<T
></a></code></li>
1636 <li><code><a href=
"#bitwise1">bitwise
<T
></a></code></li>
1639 "#unit_steppable">unit_steppable
<T
></a></code></li>
1645 <td><code><a name=
"operators2">operators
<T, U
></a></code><br>
1646 <code>operators2
<T, U
></code></td>
1650 <li><code><a href=
"#totally_ordered2">totally_ordered
<T,
1651 U
></a></code></li>
1653 <li><code><a href=
"#integer_arithmetic2">integer_arithmetic
<T,
1654 U
></a></code></li>
1656 <li><code><a href=
"#bitwise2">bitwise
<T, U
></a></code></li>
1662 <h3><a name=
"a_demo">Arithmetic Operators Demonstration
</a> and Test
1665 <p>The
<cite><a href=
"test/operators_test.cpp">operators_test.cpp
</a></cite>
1666 program demonstrates the use of the arithmetic operator templates, and
1667 can also be used to verify correct operation. Check the compiler status
1668 report for the test results with selected platforms.
</p>
1670 <h2><a name=
"deref">Dereference
</a> Operators and Iterator Helpers
</h2>
1672 <p>The
<a href=
"#iterator">iterator helper
</a> templates ease the task of
1673 creating a custom iterator. Similar to arithmetic types, a complete
1674 iterator has many operators that are
"redundant" and can be implemented
1675 in terms of the core set of operators.
</p>
1677 <p>The
<a href=
"#dereference">dereference operators
</a> were motivated by
1678 the
<a href=
"#iterator">iterator helpers
</a>, but are often useful in
1679 non-iterator contexts as well. Many of the redundant iterator operators
1680 are also arithmetic operators, so the iterator helper classes borrow many
1681 of the operators defined above. In fact, only two new operators need to
1682 be defined (the pointer-to-member
<code>operator-
></code> and the
1683 subscript
<code>operator[]
</code>)!
</p>
1685 <p>The requirements for the types used to instantiate the dereference
1686 operators are specified in terms of expressions which must be valid and
1687 their return type. The composite operator templates list their component
1688 templates, which the instantiating type must support, and possibly other
1691 <h3><a name=
"dereference">Dereference
</a> Operators
</h3>
1693 <p>All the dereference operator templates in this table accept an
1694 optional template parameter (not shown) to be used for
<a href=
1695 "#chaining">base class chaining
</a>.
</p>
1697 <table cellpadding=
"5" border=
"1" align=
"center">
1699 Dereference Operator Template Classes
1704 <table align=
"center" border=
"1">
1710 <td><code>T
</code>: operand type
</td>
1712 <td><code>P
</code>:
<code>pointer
</code> type
</td>
1716 <td><code>D
</code>:
<code>difference_type
</code></td>
1718 <td><code>R
</code>:
<code>reference
</code> type
</td>
1722 <td><code>i
</code>: object of type
<code>T
</code> (an
1725 <td><code>n
</code>: object of type
<code>D
</code> (an
1735 <th>Supplied Operations
</th>
1737 <th>Requirements
</th>
1741 <td><code><a name=
"dereferenceable">dereferenceable
<T,
1742 P
></a></code></td>
1744 <td><code>P operator-
>() const
</code></td>
1746 <td><code>*i
</code>. Address of the returned value convertible
1747 to
<code>P
</code>.
</td>
1751 <td><code><a name=
"indexable">indexable
<T, D,
1752 R
></a></code></td>
1754 <td><code>R operator[](D n) const
</code></td>
1756 <td><code>*(i
+
n)
</code>. Return of type
1757 <code>R
</code>.
</td>
1761 <h3><a name=
"grpd_iter_oprs">Grouped Iterator Operators
</a></h3>
1763 <p>There are five iterator operator class templates, each for a different
1764 category of iterator. The following table shows the operator groups for
1765 any category that a custom iterator could define. These class templates
1766 have an additional optional template parameter
<code>B
</code>, which is
1767 not shown, to support
<a href=
"#chaining">base class chaining
</a>.
</p>
1769 <table cellpadding=
"5" border=
"1" align=
"center">
1771 Iterator Operator Class Templates
1776 <table align=
"center" border=
"1">
1782 <td><code>T
</code>: operand type
</td>
1784 <td><code>P
</code>:
<code>pointer
</code> type
</td>
1788 <td><code>D
</code>:
<code>difference_type
</code></td>
1790 <td><code>R
</code>:
<code>reference
</code> type
</td>
1794 <td><code>V
</code>:
<code>value_type
</code></td>
1806 <th>Component Operator Templates
</th>
1810 <td><code><a name=
"input_iteratable">input_iteratable
<T,
1811 P
></a></code></td>
1816 "#equality_comparable1">equality_comparable
<T
></a></code></li>
1819 "#incrementable">incrementable
<T
></a></code></li>
1821 <li><code><a href=
"#dereferenceable">dereferenceable
<T,
1822 P
></a></code></li>
1829 "output_iteratable">output_iteratable
<T
></a></code></td>
1834 "#incrementable">incrementable
<T
></a></code></li>
1840 <td><code><a name=
"forward_iteratable">forward_iteratable
<T,
1841 P
></a></code></td>
1845 <li><code><a href=
"#input_iteratable">input_iteratable
<T,
1846 P
></a></code></li>
1853 "bidirectional_iteratable">bidirectional_iteratable
<T,
1854 P
></a></code></td>
1858 <li><code><a href=
"#forward_iteratable">forward_iteratable
<T,
1859 P
></a></code></li>
1862 "#decrementable">decrementable
<T
></a></code></li>
1869 "random_access_iteratable">random_access_iteratable
<T, P, D,
1870 R
></a></code></td>
1875 "#bidirectional_iteratable">bidirectional_iteratable
<T,
1876 P
></a></code></li>
1879 "#totally_ordered1">totally_ordered
<T
></a></code></li>
1881 <li><code><a href=
"#additive2">additive
<T,
1882 D
></a></code></li>
1884 <li><code><a href=
"#indexable">indexable
<T, D,
1885 R
></a></code></li>
1891 <h3><a name=
"iterator">Iterator
</a> Helpers
</h3>
1893 <p>There are also five iterator helper class templates, each
1894 corresponding to a different iterator category. These classes cannot be
1895 used for
<a href=
"#chaining">base class chaining
</a>. The following
1896 summaries show that these class templates supply both the iterator
1897 operators from the
<a href=
"#grpd_iter_oprs">iterator operator class
1898 templates
</a> and the iterator typedef's required by the C++ standard
1899 (
<code>iterator_category
</code>,
<code>value_type
</code>,
1902 <table cellpadding=
"5" border=
"1" align=
"center">
1904 Iterator Helper Class Templates
1909 <table align=
"center" border=
"1">
1915 <td><code>T
</code>: operand type
</td>
1917 <td><code>P
</code>:
<code>pointer
</code> type
</td>
1921 <td><code>D
</code>:
<code>difference_type
</code></td>
1923 <td><code>R
</code>:
<code>reference
</code> type
</td>
1927 <td><code>V
</code>:
<code>value_type
</code></td>
1929 <td><code>x1, x2
</code>: objects of type
<code>T
</code></td>
1938 <th>Operations
& Requirements
</th>
1941 <tr valign=
"baseline">
1942 <td><code><a name=
"input_iterator_helper">input_iterator_helper
<T,
1943 V, D, P, R
></a></code></td>
1946 Supports the operations and has the requirements of
1949 <li><code><a href=
"#input_iteratable">input_iteratable
<T,
1950 P
></a></code></li>
1955 <tr valign=
"baseline">
1957 "output_iterator_helper">output_iterator_helper
<T
></a></code></td>
1960 Supports the operations and has the requirements of
1964 "#output_iteratable">output_iteratable
<T
></a></code></li>
1966 See also [
<a href=
"#1">1</a>], [
<a href=
"#2">2</a>].
1970 <tr valign=
"baseline">
1972 "forward_iterator_helper">forward_iterator_helper
<T, V, D, P,
1973 R
></a></code></td>
1976 Supports the operations and has the requirements of
1979 <li><code><a href=
"#forward_iteratable">forward_iteratable
<T,
1980 P
></a></code></li>
1985 <tr valign=
"baseline">
1987 "bidirectional_iterator_helper">bidirectional_iterator_helper
<T,
1988 V, D, P, R
></a></code></td>
1991 Supports the operations and has the requirements of
1995 "#bidirectional_iteratable">bidirectional_iteratable
<T,
1996 P
></a></code></li>
2001 <tr valign=
"baseline">
2003 "random_access_iterator_helper">random_access_iterator_helper
<T,
2004 V, D, P, R
></a></code></td>
2007 Supports the operations and has the requirements of
2011 "#random_access_iteratable">random_access_iteratable
<T, P, D,
2012 R
></a></code></li>
2014 To satisfy
<cite><a href=
2015 "http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator
</a></cite>,
2016 <code>x1 - x2
</code> with return convertible to
<code>D
</code> is
2022 <h4><a name=
"iterator_helpers_notes">Iterator Helper Notes
</a></h4>
2024 <p><a name=
"1">[
1]
</a> Unlike other iterator helpers templates,
2025 <code>output_iterator_helper
</code> takes only one template parameter -
2026 the type of its target class. Although to some it might seem like an
2027 unnecessary restriction, the standard requires
2028 <code>difference_type
</code> and
<code>value_type
</code> of any output
2029 iterator to be
<code>void
</code> (
24.3.1 [lib.iterator.traits]), and
2030 <code>output_iterator_helper
</code> template respects this requirement.
2031 Also, output iterators in the standard have void
<code>pointer
</code> and
2032 <code>reference
</code> types, so the
<code>output_iterator_helper
</code>
2035 <p><a name=
"2">[
2]
</a> As self-proxying is the easiest and most common
2036 way to implement output iterators (see, for example, insert [
24.4.2] and
2037 stream iterators [
24.5] in the standard library),
2038 <code>output_iterator_helper
</code> supports the idiom by defining
2039 <code>operator*
</code> and
<code>operator++
</code> member functions which
2040 just return a non-const reference to the iterator itself. Support for
2041 self-proxying allows us, in many cases, to reduce the task of writing an
2042 output iterator to writing just two member functions - an appropriate
2043 constructor and a copy-assignment operator. For example, here is a
2044 possible implementation of
<code><a href=
2045 "../iterator/doc/function_output_iterator.html">boost::function_output_iterator
</a></code>
2048 template
<class UnaryFunction
>
2049 struct function_output_iterator
2050 : boost::output_iterator_helper
< function_output_iterator
<UnaryFunction
> >
2052 explicit function_output_iterator(UnaryFunction const
& f = UnaryFunction())
2055 template
<typename T
>
2056 function_output_iterator
& operator=(T const
& value)
2058 this-
>func(value);
2067 <p>Note that support for self-proxying does not prevent you from using
2068 <code>output_iterator_helper
</code> to ease any other, different kind of
2069 output iterator's implementation. If
2070 <code>output_iterator_helper
</code>'s target type provides its own
2071 definition of
<code>operator*
</code> or/and
<code>operator++
</code>, then
2072 these operators will get used and the ones supplied by
2073 <code>output_iterator_helper
</code> will never be instantiated.
</p>
2075 <h3><a name=
"i_demo">Iterator Demonstration
</a> and Test Program
</h3>
2077 <p>The
<cite><a href=
"test/iterators_test.cpp">iterators_test.cpp
</a></cite>
2078 program demonstrates the use of the iterator templates, and can also be
2079 used to verify correct operation. The following is the custom iterator
2080 defined in the test program. It demonstrates a correct (though trivial)
2081 implementation of the core operations that must be defined in order for
2082 the iterator helpers to
"fill in" the rest of the iterator
2087 template
<class T, class R, class P
>
2089 : public boost::random_access_iterator_helper
<
2090 test_iter
<T,R,P
>, T, std::ptrdiff_t, P, R
>
2092 typedef test_iter self;
2093 typedef R Reference;
2094 typedef std::ptrdiff_t Distance;
2097 explicit test_iter(T* i =
0);
2098 test_iter(const self
& x);
2099 self
& operator=(const self
& x);
2100 Reference operator*() const;
2101 self
& operator++();
2102 self
& operator--();
2103 self
& operator+=(Distance n);
2104 self
& operator-=(Distance n);
2105 bool operator==(const self
& x) const;
2106 bool operator
<(const self
& x) const;
2107 friend Distance operator-(const self
& x, const self
& y);
2112 <p>Check the
<a href=
"http://www.boost.org/development/testing.html">compiler status
2113 report
</a> for the test results with selected platforms.
</p>
2116 <h2><a name=
"contributors">Contributors
</a></h2>
2119 <dt><a href=
"http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams
</a></dt>
2121 <dd>Started the library and contributed the arithmetic operators in
2123 "../../boost/operators.hpp">boost/operators.hpp
</a></cite>.
</dd>
2125 <dt><a href=
"http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek
</a></dt>
2127 <dd>Contributed the
<a href=
"#deref">dereference operators and iterator
2128 helpers
</a> in
<cite><a href=
2129 "../../boost/operators.hpp">boost/operators.hpp
</a></cite>. Also
2130 contributed
<cite><a href=
2131 "iterators_test.cpp">iterators_test.cpp
</a></cite>.
</dd>
2133 <dt><a href=
"http://www.boost.org/people/aleksey_gurtovoy.htm">Aleksey
2136 <dd>Contributed the code to support
<a href=
"#chaining">base class
2137 chaining
</a> while remaining backward-compatible with old versions of
2140 <dt><a href=
"http://www.boost.org/people/beman_dawes.html">Beman Dawes
</a></dt>
2142 <dd>Contributed
<cite><a href=
2143 "test/operators_test.cpp">operators_test.cpp
</a></cite>.
</dd>
2145 <dt><a href=
"http://www.boost.org/people/daryle_walker.html">Daryle Walker
</a></dt>
2147 <dd>Contributed classes for the shift operators, equivalence, partial
2148 ordering, and arithmetic conversions. Added the grouped operator
2149 classes. Added helper classes for input and output iterators.
</dd>
2151 <dt>Helmut Zeisel
</dt>
2153 <dd>Contributed the 'left' operators and added some grouped operator
2156 <dt>Daniel Frey
</dt>
2158 <dd>Contributed the NRVO-friendly and symmetric implementation of
2159 arithmetic operators.
</dd>
2163 <h2>Note for Users of
<a name=
"old_lib_note">Older Versions
</a></h2>
2165 <p>The
<a href=
"#chaining">changes in the library interface and
2166 recommended usage
</a> were motivated by some practical issues described
2167 below. The new version of the library is still backward-compatible with
2168 the former one (so you're not
<em>forced
</em> change any existing code),
2169 but the old usage is deprecated. Though it was arguably simpler and more
2170 intuitive than using
<a href=
"#chaining">base class chaining
</a>, it has
2171 been discovered that the old practice of deriving from multiple operator
2172 templates can cause the resulting classes to be much larger than they
2173 should be. Most modern C++ compilers significantly bloat the size of
2174 classes derived from multiple empty base classes, even though the base
2175 classes themselves have no state. For instance, the size of
2176 <code>point
<int
></code> from the
<a href=
"#example">example
</a>
2177 above was
12-
24 bytes on various compilers for the Win32 platform,
2178 instead of the expected
8 bytes.
</p>
2180 <p>Strictly speaking, it was not the library's fault--the language rules
2181 allow the compiler to apply the empty base class optimization in that
2182 situation. In principle an arbitrary number of empty base classes can be
2183 allocated at the same offset, provided that none of them have a common
2184 ancestor (see section
10.5 [class.derived] paragraph
5 of the standard).
2185 But the language definition also doesn't
<em>require
</em> implementations
2186 to do the optimization, and few if any of today's compilers implement it
2187 when multiple inheritance is involved. What's worse, it is very unlikely
2188 that implementors will adopt it as a future enhancement to existing
2189 compilers, because it would break binary compatibility between code
2190 generated by two different versions of the same compiler. As Matt Austern
2191 said,
"One of the few times when you have the freedom to do this sort of
2192 thing is when you're targeting a new architecture...". On the other hand,
2193 many common compilers will use the empty base optimization for single
2194 inheritance hierarchies.
</p>
2196 <p>Given the importance of the issue for the users of the library (which
2197 aims to be useful for writing light-weight classes like
2198 <code>MyInt
</code> or
<code>point
<></code>), and the forces
2199 described above, we decided to change the library interface so that the
2200 object size bloat could be eliminated even on compilers that support only
2201 the simplest form of the empty base class optimization. The current
2202 library interface is the result of those changes. Though the new usage is
2203 a bit more complicated than the old one, we think it's worth it to make
2204 the library more useful in real world. Alexy Gurtovoy contributed the
2205 code which supports the new usage idiom while allowing the library remain
2206 backward-compatible.
</p>
2209 <p>Revised:
7 Aug
2008</p>
2211 <p>Copyright
© Beman Dawes, David Abrahams,
1999-
2001.
</p>
2212 <p>Copyright
© Daniel Frey,
2002-
2009.
</p>
2213 <p>Use, modification, and distribution is subject to the Boost Software
2214 License, Version
1.0. (See accompanying file
2215 <a href=
"../../LICENSE_1_0.txt">LICENSE_1_0.txt
</a> or copy at
2216 <a href=
"http://www.boost.org/LICENSE_1_0.txt">
2217 www.boost.org/LICENSE_1_0.txt
</a>)
</p>