operators.htm 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
  2. <html>
  3. <head>
  4. <meta name="generator" content=
  5. "HTML Tidy for Windows (vers 1st August 2002), see www.w3.org">
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  7. <title>Header &lt;boost/operators.hpp&gt; Documentation</title>
  8. </head>
  9. <body text="black" bgcolor="white" link="blue" vlink="purple" alink="red">
  10. <h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align=
  11. "middle" width="277" height="86">Header <cite>&lt;<a href=
  12. "../../boost/operators.hpp">boost/operators.hpp</a>&gt;</cite></h1>
  13. <p>The header <cite>&lt;<a href=
  14. "../../boost/operators.hpp">boost/operators.hpp</a>&gt;</cite> supplies
  15. several sets of class templates (in namespace <code>boost</code>). These
  16. templates define operators at namespace scope in terms of a minimal
  17. number of fundamental operators provided by the class.</p>
  18. <h2><a name="contents">Contents</a></h2>
  19. <ul>
  20. <li><a href="#contents">Contents</a></li>
  21. <li>
  22. <a href="#rationale">Rationale</a>
  23. <ul>
  24. <li><a href="#semantics">Summary of Template Semantics</a></li>
  25. <li><a href="#concepts_note">Use of <i>concepts</i></a></li>
  26. </ul>
  27. </li>
  28. <li>
  29. <a href="#usage">Usage</a>
  30. <ul>
  31. <li>
  32. <a href="#two_arg">Two-Argument Template Forms</a>
  33. <ul>
  34. <li><a href="#two_arg_gen">General Considerations</a></li>
  35. <li><a href="#mixed_arithmetics">Mixed arithmetics</a></li>
  36. </ul>
  37. </li>
  38. <li><a href="#chaining">Base Class Chaining and Object
  39. Size</a></li>
  40. <li><a href="#explicit_instantiation">Separate, Explicit
  41. Instantiation</a></li>
  42. <li><a href="#portability">Requirement Portability</a></li>
  43. </ul>
  44. </li>
  45. <li><a href="#example">Example</a></li>
  46. <li>
  47. <a href="#arithmetic">Arithmetic operators</a>
  48. <ul>
  49. <li>
  50. <a href="#smpl_oprs">Simple Arithmetic Operators</a>
  51. <ul>
  52. <li><a href="#ordering">Ordering Note</a></li>
  53. <li><a href="#symmetry">Symmetry Note</a></li>
  54. </ul>
  55. </li>
  56. <li><a href="#grpd_oprs">Grouped Arithmetic Operators</a></li>
  57. <li><a href="#ex_oprs">Example Templates</a></li>
  58. <li><a href="#a_demo">Arithmetic Operators Demonstration and Test
  59. Program</a></li>
  60. </ul>
  61. </li>
  62. <li>
  63. <a href="#deref">Dereference Operators and Iterator Helpers</a>
  64. <ul>
  65. <li><a href="#dereference">Dereference operators</a></li>
  66. <li><a href="#grpd_iter_oprs">Grouped Iterator Operators</a></li>
  67. <li>
  68. <a href="#iterator">Iterator Helpers</a>
  69. <ul>
  70. <li><a href="#iterator_helpers_notes">Iterator Helper
  71. Notes</a></li>
  72. </ul>
  73. </li>
  74. <li><a href="#i_demo">Iterator Demonstration and Test
  75. Program</a></li>
  76. </ul>
  77. </li>
  78. <li><a href="#contributors">Contributors</a></li>
  79. <li><a href="#old_lib_note">Note for Users of Older Versions</a></li>
  80. </ul>
  81. <h2><a name="rationale">Rationale</a></h2>
  82. <p>Overloaded operators for class types typically occur in groups. If you
  83. can write <code>x&nbsp;+&nbsp;y</code>, you probably also want to be able
  84. to write <code>x += y</code>. If you can write <code>x &lt; y,</code> you
  85. also want <code>x &gt; y, x &gt;= y,</code> and <code>x &lt;= y</code>.
  86. Moreover, unless your class has really surprising behavior, some of these
  87. related operators can be defined in terms of others (e.g. <code>x &gt;= y
  88. is equivalent to !(x &lt; y)</code>). Replicating this boilerplate for
  89. multiple classes is both tedious and error-prone. The <cite><a href=
  90. "../../boost/operators.hpp">boost/operators.hpp</a></cite> templates help
  91. by generating operators for you at namespace scope based on other
  92. operators you've defined in your class.</p>
  93. <p>If, for example, you declare a class like this:</p>
  94. <blockquote>
  95. <pre>
  96. class MyInt
  97. : boost::operators&lt;MyInt&gt;
  98. {
  99. bool operator&lt;(const MyInt&amp; x) const;
  100. bool operator==(const MyInt&amp; x) const;
  101. MyInt&amp; operator+=(const MyInt&amp; x);
  102. MyInt&amp; operator-=(const MyInt&amp; x);
  103. MyInt&amp; operator*=(const MyInt&amp; x);
  104. MyInt&amp; operator/=(const MyInt&amp; x);
  105. MyInt&amp; operator%=(const MyInt&amp; x);
  106. MyInt&amp; operator|=(const MyInt&amp; x);
  107. MyInt&amp; operator&amp;=(const MyInt&amp; x);
  108. MyInt&amp; operator^=(const MyInt&amp; x);
  109. MyInt&amp; operator++();
  110. MyInt&amp; operator--();
  111. };
  112. </pre>
  113. </blockquote>
  114. <p>then the <code><a href="#operators1">operators&lt;&gt;</a></code>
  115. template adds more than a dozen additional operators, such as
  116. <code>operator&gt;</code>, <code>&lt;=</code>, <code>&gt;=</code>, and
  117. (binary) <code>+</code>. <a href="#two_arg">Two-argument forms</a> of the
  118. templates are also provided to allow interaction with other types.</p>
  119. <h3>Summary of Template <a name="semantics">Semantics</a></h3>
  120. <ol>
  121. <li>Each operator template completes the concept(s) it describes by
  122. defining overloaded operators for its target class.</li>
  123. <li>The name of an operator class template indicates the <a href=
  124. "#concepts_note">concept</a> that its target class will model.</li>
  125. <li>Usually, the target class uses an instantation of the operator
  126. class template as a base class. Some operator templates support an <a
  127. href="#explicit_instantiation">alternate method</a>.</li>
  128. <li>The concept can be compound, <i>i.e.</i> it may represent a common
  129. combination of other, simpler concepts.</li>
  130. <li>Most operator templates require their target class to support
  131. operations related to the operators supplied by the template. In
  132. accordance with widely accepted <a href=
  133. "http://www.gotw.ca/gotw/004.htm">coding style recommendations</a>, the
  134. target class is often required to supply the assignment counterpart
  135. operator of the concept's "main operator." For example, the
  136. <code>addable</code> template requires <code>operator+=(T
  137. const&amp;)</code> and in turn supplies <code>operator+(T const&amp;, T
  138. const&amp;)</code>.</li>
  139. </ol>
  140. <h3>Use of <i><a name="concepts_note">concepts</a></i></h3>
  141. <p>The discussed concepts are not necessarily the standard library's
  142. concepts (CopyConstructible, <i>etc.</i>), although some of them could
  143. be; they are what we call <i>concepts with a small 'c'</i>. In
  144. particular, they are different from the former ones in that they <em>do
  145. not</em> describe precise semantics of the operators they require to be
  146. defined, except the requirements that (a) the semantics of the operators
  147. grouped in one concept should be consistent (<i>e.g.</i> effects of
  148. evaluating of <code>a += b</code> and
  149. <code>a&nbsp;=&nbsp;a&nbsp;+&nbsp;b</code> expressions should be the
  150. same), and (b) that the return types of the operators should follow
  151. semantics of return types of corresponding operators for built-in types
  152. (<i>e.g.</i> <code>operator&lt;</code> should return a type convertible
  153. to <code>bool</code>, and <code>T::operator-=</code> should return type
  154. convertible to <code>T</code>). Such "loose" requirements make operators
  155. library applicable to broader set of target classes from different
  156. domains, <i>i.e.</i> eventually more useful.</p>
  157. <h2><a name="usage">Usage</a></h2>
  158. <h3><a name="two_arg">Two-Argument</a> Template Forms</h3>
  159. <h4><a name="two_arg_gen">General Considerations</a></h4>
  160. <p>The arguments to a binary operator commonly have identical types, but
  161. it is not unusual to want to define operators which combine different
  162. types. For <a href="#example">example</a>, one might want to multiply a
  163. mathematical vector by a scalar. The two-argument template forms of the
  164. arithmetic operator templates are supplied for this purpose. When
  165. applying the two-argument form of a template, the desired return type of
  166. the operators typically determines which of the two types in question
  167. should be derived from the operator template. For example, if the result
  168. of <code>T&nbsp;+&nbsp;U</code> is of type <code>T</code>, then
  169. <code>T</code> (not <code>U</code>) should be derived from <code><a href=
  170. "#addable2">addable&lt;T, U&gt;</a></code>. The comparison templates
  171. (<code><a href="#less_than_comparable2">less_than_comparable&lt;T,
  172. U&gt;</a></code>, <code><a href=
  173. "#equality_comparable2">equality_comparable&lt;T, U&gt;</a></code>,
  174. <code><a href="#equivalent2">equivalent&lt;T, U&gt;</a></code>, and
  175. <code><a href="#partially_ordered2">partially_ordered&lt;T,
  176. U&gt;</a></code>) are exceptions to this guideline, since the return type
  177. of the operators they define is <code>bool</code>.</p>
  178. <p>On compilers which do not support partial specialization, the
  179. two-argument forms must be specified by using the names shown below with
  180. the trailing <code>'2'</code>. The single-argument forms with the
  181. trailing <code>'1'</code> are provided for symmetry and to enable certain
  182. applications of the <a href="#chaining">base class chaining</a>
  183. technique.</p>
  184. <h4><a name="mixed_arithmetics">Mixed Arithmetics</a></h4>
  185. <p>Another application of the two-argument template forms is for mixed
  186. arithmetics between a type <code>T</code> and a type <code>U</code> that
  187. is convertible to <code>T</code>. In this case there are two ways where
  188. the two-argument template forms are helpful: one is to provide the
  189. respective signatures for operator overloading, the second is
  190. performance.</p>
  191. <p>With respect to the operator overloading assume <i>e.g.</i> that
  192. <code>U</code> is <code>int</code>, that <code>T</code> is an
  193. user-defined unlimited integer type, and that <code>double
  194. operator-(double, const T&amp;)</code> exists. If one wants to compute
  195. <code>int - T</code> and does not provide <code>T operator-(int, const
  196. T&amp;)</code>, the compiler will consider <code>double operator-(double,
  197. const T&amp;)</code> to be a better match than <code>T operator-(const
  198. T&amp;, const T&amp;)</code>, which will probably be different from the
  199. user's intention. To define a complete set of operator signatures,
  200. additional 'left' forms of the two-argument template forms are provided
  201. (<code><a href="#subtractable2_left">subtractable2_left&lt;T,
  202. U&gt;</a></code>, <code><a href="#dividable2_left">dividable2_left&lt;T,
  203. U&gt;</a></code>, <code><a href="#modable2_left">modable2_left&lt;T,
  204. U&gt;</a></code>) that define the signatures for non-commutative
  205. operators where <code>U</code> appears on the left hand side
  206. (<code>operator-(const U&amp;, const T&amp;)</code>,
  207. <code>operator/(const U&amp;, const T&amp;)</code>, <code>operator%(const
  208. U&amp;, const T&amp;)</code>).</p>
  209. <p>With respect to the performance observe that when one uses the single
  210. type binary operator for mixed type arithmetics, the type <code>U</code>
  211. argument has to be converted to type <code>T</code>. In practice,
  212. however, there are often more efficient implementations of, say
  213. <code>T::operator-=(const U&amp;)</code> that avoid unnecessary
  214. conversions from <code>U</code> to <code>T</code>. The two-argument
  215. template forms of the arithmetic operator create additional operator
  216. interfaces that use these more efficient implementations. There is,
  217. however, no performance gain in the 'left' forms: they still need a
  218. conversion from <code>U</code> to <code>T</code> and have an
  219. implementation equivalent to the code that would be automatically created
  220. by the compiler if it considered the single type binary operator to be
  221. the best match.</p>
  222. <h3>Base Class <a name="chaining">Chaining</a> and Object Size</h3>
  223. <p>Every operator class template, except the <a href=
  224. "#ex_oprs">arithmetic examples</a> and the <a href="#iterator">iterator
  225. helpers</a>, has an additional, but optional, template type parameter
  226. <code>B</code>. This parameter will be a publicly-derived base class of
  227. the instantiated template. This means it must be a class type. It can be
  228. used to avoid the bloating of object sizes that is commonly associated
  229. with multiple-inheritance from several empty base classes (see the <a
  230. href="#old_lib_note">note for users of older versions</a> for more
  231. details). To provide support for a group of operators, use the
  232. <code>B</code> parameter to chain operator templates into a single-base
  233. class hierarchy, demostrated in the <a href="#example">usage example</a>.
  234. The technique is also used by the composite operator templates to group
  235. operator definitions. If a chain becomes too long for the compiler to
  236. support, try replacing some of the operator templates with a single
  237. grouped operator template that chains the old templates together; the
  238. length limit only applies to the number of templates directly in the
  239. chain, not those hidden in group templates.</p>
  240. <p><strong>Caveat:</strong> to chain to a base class which is
  241. <em>not</em> a Boost operator template when using the <a href=
  242. "#two_arg">single-argument form</a> of a Boost operator template, you
  243. must specify the operator template with the trailing <code>'1'</code> in
  244. its name. Otherwise the library will assume you mean to define a binary
  245. operation combining the class you intend to use as a base class and the
  246. class you're deriving.</p>
  247. <h3>Separate, <a name="explicit_instantiation">Explicit
  248. Instantiation</a></h3>
  249. <p>On some compilers (<i>e.g.</i> Borland, GCC) even single-inheritance
  250. seems to cause an increase in object size in some cases. If you are not
  251. defining a class template, you may get better object-size performance by
  252. avoiding derivation altogether, and instead explicitly instantiating the
  253. operator template as follows:</p>
  254. <blockquote>
  255. <pre>
  256. class myclass // lose the inheritance...
  257. {
  258. //...
  259. };
  260. // explicitly instantiate the operators I need.
  261. template struct less_than_comparable&lt;myclass&gt;;
  262. template struct equality_comparable&lt;myclass&gt;;
  263. template struct incrementable&lt;myclass&gt;;
  264. template struct decrementable&lt;myclass&gt;;
  265. template struct addable&lt;myclass,long&gt;;
  266. template struct subtractable&lt;myclass,long&gt;;
  267. </pre>
  268. </blockquote>
  269. <p>Note that some operator templates cannot use this workaround and must
  270. be a base class of their primary operand type. Those templates define
  271. operators which must be member functions, and the workaround needs the
  272. operators to be independent friend functions. The relevant templates
  273. are:</p>
  274. <ul>
  275. <li><code><a href=
  276. "#dereferenceable">dereferenceable&lt;&gt;</a></code></li>
  277. <li><code><a href="#indexable">indexable&lt;&gt;</a></code></li>
  278. <li>Any composite operator template that includes at least one of the
  279. above</li>
  280. </ul>
  281. <p>As Daniel Kr&uuml;gler pointed out, this technique violates 14.6.5/2
  282. and is thus non-portable. The reasoning is, that the operators injected
  283. by the instantiation of e.g.
  284. <code>less_than_comparable&lt;myclass&gt;</code> can not be found
  285. by ADL according to the rules given by 3.4.2/2, since myclass is
  286. not an associated class of
  287. <code>less_than_comparable&lt;myclass&gt;</code>.
  288. Thus only use this technique if all else fails.</p>
  289. <h3>Requirement <a name="portability">Portability</a></h3>
  290. <p>Many compilers (<i>e.g.</i> MSVC 6.3, GCC 2.95.2) will not enforce the
  291. requirements in the operator template tables unless the operations which
  292. depend on them are actually used. This is not standard-conforming
  293. behavior. In particular, although it would be convenient to derive all
  294. your classes which need binary operators from the <code><a href=
  295. "#operators1">operators&lt;&gt;</a></code> and <code><a href=
  296. "#operators2">operators2&lt;&gt;</a></code> templates, regardless of
  297. whether they implement all the requirements of those templates, this
  298. shortcut is not portable. Even if this currently works with your
  299. compiler, it may not work later.</p>
  300. <h2><a name="example">Example</a></h2>
  301. <p>This example shows how some of the <a href="#arithmetic">arithmetic
  302. operator templates</a> can be used with a geometric point class
  303. (template).</p>
  304. <pre>
  305. template &lt;class T&gt;
  306. class point // note: private inheritance is OK here!
  307. : boost::addable&lt; point&lt;T&gt; // point + point
  308. , boost::subtractable&lt; point&lt;T&gt; // point - point
  309. , boost::dividable2&lt; point&lt;T&gt;, T // point / T
  310. , boost::multipliable2&lt; point&lt;T&gt;, T // point * T, T * point
  311. &gt; &gt; &gt; &gt;
  312. {
  313. public:
  314. point(T, T);
  315. T x() const;
  316. T y() const;
  317. point operator+=(const point&amp;);
  318. // point operator+(point, const point&amp;) automatically
  319. // generated by addable.
  320. point operator-=(const point&amp;);
  321. // point operator-(point, const point&amp;) automatically
  322. // generated by subtractable.
  323. point operator*=(T);
  324. // point operator*(point, const T&amp;) and
  325. // point operator*(const T&amp;, point) auto-generated
  326. // by multipliable.
  327. point operator/=(T);
  328. // point operator/(point, const T&amp;) auto-generated
  329. // by dividable.
  330. private:
  331. T x_;
  332. T y_;
  333. };
  334. // now use the point&lt;&gt; class:
  335. template &lt;class T&gt;
  336. T length(const point&lt;T&gt; p)
  337. {
  338. return sqrt(p.x()*p.x() + p.y()*p.y());
  339. }
  340. const point&lt;float&gt; right(0, 1);
  341. const point&lt;float&gt; up(1, 0);
  342. const point&lt;float&gt; pi_over_4 = up + right;
  343. const point&lt;float&gt; pi_over_4_normalized = pi_over_4 / length(pi_over_4);
  344. </pre>
  345. <h2><a name="arithmetic">Arithmetic</a> Operators</h2>
  346. <p>The arithmetic operator templates ease the task of creating a custom
  347. numeric type. Given a core set of operators, the templates add related
  348. operators to the numeric class. These operations are like the ones the
  349. standard arithmetic types have, and may include comparisons, adding,
  350. incrementing, logical and bitwise manipulations, <i>etc</i>. Further,
  351. since most numeric types need more than one of these operators, some
  352. templates are provided to combine several of the basic operator templates
  353. in one declaration.</p>
  354. <p>The requirements for the types used to instantiate the simple operator
  355. templates are specified in terms of expressions which must be valid and
  356. the expression's return type. The composite operator templates only list
  357. what other templates they use. The supplied operations and requirements
  358. of the composite operator templates can be inferred from the operations
  359. and requirements of the listed components.</p>
  360. <h3><a name="smpl_oprs">Simple Arithmetic Operators</a></h3>
  361. <p>These templates are "simple" since they provide operators based on a
  362. single operation the base type has to provide. They have an additional
  363. optional template parameter <code>B</code>, which is not shown, for the
  364. <a href="#chaining">base class chaining</a> technique.</p>
  365. <p>The primary operand type <code>T</code> needs to be of class type,
  366. built-in types are not supported.</p>
  367. <table cellpadding="5" border="1" align="center">
  368. <caption>
  369. Simple Arithmetic Operator Template Classes
  370. </caption>
  371. <tr>
  372. <td colspan="3">
  373. <table align="center" border="1">
  374. <caption>
  375. <em>Key</em>
  376. </caption>
  377. <tr>
  378. <td><code>T</code>: primary operand type</td>
  379. <td><code>U</code>: alternate operand type</td>
  380. </tr>
  381. <tr>
  382. <td><code>t</code>, <code>t1</code>: values of type
  383. <code>T</code></td>
  384. <td><code>u</code>: value of type <code>U</code></td>
  385. </tr>
  386. </table>
  387. </td>
  388. </tr>
  389. <tr>
  390. <th>Template</th>
  391. <th>Supplied Operations</th>
  392. <th>Requirements</th>
  393. </tr>
  394. <tr>
  395. <td><code><a name=
  396. "less_than_comparable1">less_than_comparable&lt;T&gt;</a></code><br>
  397. <code>less_than_comparable1&lt;T&gt;</code></td>
  398. <td><code>bool operator&gt;(const T&amp;, const T&amp;)</code><br>
  399. <code>bool operator&lt;=(const T&amp;, const T&amp;)</code><br>
  400. <code>bool operator&gt;=(const T&amp;, const T&amp;)</code></td>
  401. <td><code>t &lt; t1</code>.<br>
  402. Return convertible to <code>bool</code>. See the <a href=
  403. "#ordering">Ordering Note</a>.</td>
  404. </tr>
  405. <tr>
  406. <td><code><a name="less_than_comparable2">less_than_comparable&lt;T,
  407. U&gt;</a></code><br>
  408. <code>less_than_comparable2&lt;T, U&gt;</code></td>
  409. <td><code>bool operator&lt;=(const T&amp;, const U&amp;)</code><br>
  410. <code>bool operator&gt;=(const T&amp;, const U&amp;)</code><br>
  411. <code>bool operator&gt;(const U&amp;, const T&amp;)</code><br>
  412. <code>bool operator&lt;(const U&amp;, const T&amp;)</code><br>
  413. <code>bool operator&lt;=(const U&amp;, const T&amp;)</code><br>
  414. <code>bool operator&gt;=(const U&amp;, const T&amp;)</code></td>
  415. <td><code>t &lt; u</code>. <code>t &gt; u</code>.<br>
  416. Returns convertible to <code>bool</code>. See the <a href=
  417. "#ordering">Ordering Note</a>.</td>
  418. </tr>
  419. <tr>
  420. <td><code><a name=
  421. "equality_comparable1">equality_comparable&lt;T&gt;</a></code><br>
  422. <code>equality_comparable1&lt;T&gt;</code></td>
  423. <td><code>bool operator!=(const T&amp;, const T&amp;)</code></td>
  424. <td><code>t == t1</code>.<br>
  425. Return convertible to <code>bool</code>.</td>
  426. </tr>
  427. <tr>
  428. <td><code><a name="equality_comparable2">equality_comparable&lt;T,
  429. U&gt;</a></code><br>
  430. <code>equality_comparable2&lt;T, U&gt;</code></td>
  431. <td><code>bool operator==(const U&amp;, const T&amp;)</code><br>
  432. <code>bool operator!=(const U&amp;, const T&amp;)</code><br>
  433. <code>bool operator!=(const T&amp;, const U&amp;)</code></td>
  434. <td><code>t == u</code>.<br>
  435. Return convertible to <code>bool</code>.</td>
  436. </tr>
  437. <tr>
  438. <td><code><a name="addable1">addable&lt;T&gt;</a></code><br>
  439. <code>addable1&lt;T&gt;</code></td>
  440. <td><code>T operator+(const T&amp;, const T&amp;)</code></td>
  441. <td><code>T temp(t); temp += t1</code>.<br>
  442. Return convertible to <code>T</code>. See the <a href=
  443. "#symmetry">Symmetry Note</a>.</td>
  444. </tr>
  445. <tr>
  446. <td><code><a name="addable2">addable&lt;T, U&gt;</a></code><br>
  447. <code>addable2&lt;T, U&gt;</code></td>
  448. <td><code>T operator+(const T&amp;, const U&amp;)</code><br>
  449. <code>T operator+(const U&amp;, const T&amp; )</code></td>
  450. <td><code>T temp(t); temp += u</code>.<br>
  451. Return convertible to <code>T</code>. See the <a href=
  452. "#symmetry">Symmetry Note</a>.</td>
  453. </tr>
  454. <tr>
  455. <td><code><a name=
  456. "subtractable1">subtractable&lt;T&gt;</a></code><br>
  457. <code>subtractable1&lt;T&gt;</code></td>
  458. <td><code>T operator-(const T&amp;, const T&amp;)</code></td>
  459. <td><code>T temp(t); temp -= t1</code>.<br>
  460. Return convertible to <code>T</code>. See the <a href=
  461. "#symmetry">Symmetry Note</a>.</td>
  462. </tr>
  463. <tr>
  464. <td><code><a name="subtractable2">subtractable&lt;T,
  465. U&gt;</a></code><br>
  466. <code>subtractable2&lt;T, U&gt;</code></td>
  467. <td><code>T operator-(const T&amp;, const U&amp;)</code></td>
  468. <td><code>T temp(t); temp -= u</code>.<br>
  469. Return convertible to <code>T</code>. See the <a href=
  470. "#symmetry">Symmetry Note</a>.</td>
  471. </tr>
  472. <tr>
  473. <td><code><a name="subtractable2_left">subtractable2_left&lt;T,
  474. U&gt;</a></code></td>
  475. <td><code>T operator-(const U&amp;, const T&amp;)</code></td>
  476. <td><code>T temp(u); temp -= t</code>.<br>
  477. Return convertible to <code>T</code>.</td>
  478. </tr>
  479. <tr>
  480. <td><code><a name=
  481. "multipliable1">multipliable&lt;T&gt;</a></code><br>
  482. <code>multipliable1&lt;T&gt;</code></td>
  483. <td><code>T operator*(const T&amp;, const T&amp;)</code></td>
  484. <td><code>T temp(t); temp *= t1</code>.<br>
  485. Return convertible to <code>T</code>. See the <a href=
  486. "#symmetry">Symmetry Note</a>.</td>
  487. </tr>
  488. <tr>
  489. <td><code><a name="multipliable2">multipliable&lt;T,
  490. U&gt;</a></code><br>
  491. <code>multipliable2&lt;T, U&gt;</code></td>
  492. <td><code>T operator*(const T&amp;, const U&amp;)</code><br>
  493. <code>T operator*(const U&amp;, const T&amp;)</code></td>
  494. <td><code>T temp(t); temp *= u</code>.<br>
  495. Return convertible to <code>T</code>. See the <a href=
  496. "#symmetry">Symmetry Note</a>.</td>
  497. </tr>
  498. <tr>
  499. <td><code><a name="dividable1">dividable&lt;T&gt;</a></code><br>
  500. <code>dividable1&lt;T&gt;</code></td>
  501. <td><code>T operator/(const T&amp;, const T&amp;)</code></td>
  502. <td><code>T temp(t); temp /= t1</code>.<br>
  503. Return convertible to <code>T</code>. See the <a href=
  504. "#symmetry">Symmetry Note</a>.</td>
  505. </tr>
  506. <tr>
  507. <td><code><a name="dividable2">dividable&lt;T, U&gt;</a></code><br>
  508. <code>dividable2&lt;T, U&gt;</code></td>
  509. <td><code>T operator/(const T&amp;, const U&amp;)</code></td>
  510. <td><code>T temp(t); temp /= u</code>.<br>
  511. Return convertible to <code>T</code>. See the <a href=
  512. "#symmetry">Symmetry Note</a>.</td>
  513. </tr>
  514. <tr>
  515. <td><code><a name="dividable2_left">dividable2_left&lt;T,
  516. U&gt;</a></code></td>
  517. <td><code>T operator/(const U&amp;, const T&amp;)</code></td>
  518. <td><code>T temp(u); temp /= t</code>.<br>
  519. Return convertible to <code>T</code>.</td>
  520. </tr>
  521. <tr>
  522. <td><code><a name="modable1">modable&lt;T&gt;</a></code><br>
  523. <code>modable1&lt;T&gt;</code></td>
  524. <td><code>T operator%(const T&amp;, const T&amp;)</code></td>
  525. <td><code>T temp(t); temp %= t1</code>.<br>
  526. Return convertible to <code>T</code>. See the <a href=
  527. "#symmetry">Symmetry Note</a>.</td>
  528. </tr>
  529. <tr>
  530. <td><code><a name="modable2">modable&lt;T, U&gt;</a></code><br>
  531. <code>modable2&lt;T, U&gt;</code></td>
  532. <td><code>T operator%(const T&amp;, const U&amp;)</code></td>
  533. <td><code>T temp(t); temp %= u</code>.<br>
  534. Return convertible to <code>T</code>. See the <a href=
  535. "#symmetry">Symmetry Note</a>.</td>
  536. </tr>
  537. <tr>
  538. <td><code><a name="modable2_left">modable2_left&lt;T,
  539. U&gt;</a></code></td>
  540. <td><code>T operator%(const U&amp;, const T&amp;)</code></td>
  541. <td><code>T temp(u); temp %= t</code>.<br>
  542. Return convertible to <code>T</code>.</td>
  543. </tr>
  544. <tr>
  545. <td><code><a name="orable1">orable&lt;T&gt;</a></code><br>
  546. <code>orable1&lt;T&gt;</code></td>
  547. <td><code>T operator|(const T&amp;, const T&amp;)</code></td>
  548. <td><code>T temp(t); temp |= t1</code>.<br>
  549. Return convertible to <code>T</code>. See the <a href=
  550. "#symmetry">Symmetry Note</a>.</td>
  551. </tr>
  552. <tr>
  553. <td><code><a name="orable2">orable&lt;T, U&gt;</a></code><br>
  554. <code>orable2&lt;T, U&gt;</code></td>
  555. <td><code>T operator|(const T&amp;, const U&amp;)</code><br>
  556. <code>T operator|(const U&amp;, const T&amp;)</code></td>
  557. <td><code>T temp(t); temp |= u</code>.<br>
  558. Return convertible to <code>T</code>. See the <a href=
  559. "#symmetry">Symmetry Note</a>.</td>
  560. </tr>
  561. <tr>
  562. <td><code><a name="andable1">andable&lt;T&gt;</a></code><br>
  563. <code>andable1&lt;T&gt;</code></td>
  564. <td><code>T operator&amp;(const T&amp;, const T&amp;)</code></td>
  565. <td><code>T temp(t); temp &amp;= t1</code>.<br>
  566. Return convertible to <code>T</code>. See the <a href=
  567. "#symmetry">Symmetry Note</a>.</td>
  568. </tr>
  569. <tr>
  570. <td><code><a name="andable2">andable&lt;T, U&gt;</a></code><br>
  571. <code>andable2&lt;T, U&gt;</code></td>
  572. <td><code>T operator&amp;(const T&amp;, const U&amp;)</code><br>
  573. <code>T operator&amp;(const U&amp;, const T&amp;)</code></td>
  574. <td><code>T temp(t); temp &amp;= u</code>.<br>
  575. Return convertible to <code>T</code>. See the <a href=
  576. "#symmetry">Symmetry Note</a>.</td>
  577. </tr>
  578. <tr>
  579. <td><code><a name="xorable1">xorable&lt;T&gt;</a></code><br>
  580. <code>xorable1&lt;T&gt;</code></td>
  581. <td><code>T operator^(const T&amp;, const T&amp;)</code></td>
  582. <td><code>T temp(t); temp ^= t1</code>.<br>
  583. Return convertible to <code>T</code>. See the <a href=
  584. "#symmetry">Symmetry Note</a>.</td>
  585. </tr>
  586. <tr>
  587. <td><code><a name="xorable2">xorable&lt;T, U&gt;</a></code><br>
  588. <code>xorable2&lt;T, U&gt;</code></td>
  589. <td><code>T operator^(const T&amp;, const U&amp;)</code><br>
  590. <code>T operator^(const U&amp;, const T&amp;)</code></td>
  591. <td><code>T temp(t); temp ^= u</code>.<br>
  592. Return convertible to <code>T</code>. See the <a href=
  593. "#symmetry">Symmetry Note</a>.</td>
  594. </tr>
  595. <tr>
  596. <td><code><a name=
  597. "incrementable">incrementable&lt;T&gt;</a></code></td>
  598. <td><code>T operator++(T&amp;, int)</code></td>
  599. <td><code>T temp(t); ++t</code><br>
  600. Return convertible to <code>T</code>.</td>
  601. </tr>
  602. <tr>
  603. <td><code><a name=
  604. "decrementable">decrementable&lt;T&gt;</a></code></td>
  605. <td><code>T operator--(T&amp;, int)</code></td>
  606. <td><code>T temp(t); --t;</code><br>
  607. Return convertible to <code>T</code>.</td>
  608. </tr>
  609. <tr>
  610. <td><code><a name=
  611. "left_shiftable1">left_shiftable&lt;T&gt;</a></code><br>
  612. <code>left_shiftable1&lt;T&gt;</code></td>
  613. <td><code>T operator&lt;&lt;(const T&amp;, const T&amp;)</code></td>
  614. <td><code>T temp(t); temp &lt;&lt;= t1</code>.<br>
  615. Return convertible to <code>T</code>. See the <a href=
  616. "#symmetry">Symmetry Note</a>.</td>
  617. </tr>
  618. <tr>
  619. <td><code><a name="left_shiftable2">left_shiftable&lt;T,
  620. U&gt;</a></code><br>
  621. <code>left_shiftable2&lt;T, U&gt;</code></td>
  622. <td><code>T operator&lt;&lt;(const T&amp;, const U&amp;)</code></td>
  623. <td><code>T temp(t); temp &lt;&lt;= u</code>.<br>
  624. Return convertible to <code>T</code>. See the <a href=
  625. "#symmetry">Symmetry Note</a>.</td>
  626. </tr>
  627. <tr>
  628. <td><code><a name=
  629. "right_shiftable1">right_shiftable&lt;T&gt;</a></code><br>
  630. <code>right_shiftable1&lt;T&gt;</code></td>
  631. <td><code>T operator&gt;&gt;(const T&amp;, const T&amp;)</code></td>
  632. <td><code>T temp(t); temp &gt;&gt;= t1</code>.<br>
  633. Return convertible to <code>T</code>. See the <a href=
  634. "#symmetry">Symmetry Note</a>.</td>
  635. </tr>
  636. <tr>
  637. <td><code><a name="right_shiftable2">right_shiftable&lt;T,
  638. U&gt;</a></code><br>
  639. <code>right_shiftable2&lt;T, U&gt;</code></td>
  640. <td><code>T operator&gt;&gt;(const T&amp;, const U&amp;)</code></td>
  641. <td><code>T temp(t); temp &gt;&gt;= u</code>.<br>
  642. Return convertible to <code>T</code>. See the <a href=
  643. "#symmetry">Symmetry Note</a>.</td>
  644. </tr>
  645. <tr>
  646. <td><code><a name="equivalent1">equivalent&lt;T&gt;</a></code><br>
  647. <code>equivalent1&lt;T&gt;</code></td>
  648. <td><code>bool operator==(const T&amp;, const T&amp;)</code></td>
  649. <td><code>t &lt; t1</code>.<br>
  650. Return convertible to <code>bool</code>. See the <a href=
  651. "#ordering">Ordering Note</a>.</td>
  652. </tr>
  653. <tr>
  654. <td><code><a name="equivalent2">equivalent&lt;T, U&gt;</a></code><br>
  655. <code>equivalent2&lt;T, U&gt;</code></td>
  656. <td><code>bool operator==(const T&amp;, const U&amp;)</code></td>
  657. <td><code>t &lt; u</code>. <code>t &gt; u</code>.<br>
  658. Returns convertible to <code>bool</code>. See the <a href=
  659. "#ordering">Ordering Note</a>.</td>
  660. </tr>
  661. <tr>
  662. <td><code><a name=
  663. "partially_ordered1">partially_ordered&lt;T&gt;</a></code><br>
  664. <code>partially_ordered1&lt;T&gt;</code></td>
  665. <td><code>bool operator&gt;(const T&amp;, const T&amp;)</code><br>
  666. <code>bool operator&lt;=(const T&amp;, const T&amp;)</code><br>
  667. <code>bool operator&gt;=(const T&amp;, const T&amp;)</code></td>
  668. <td><code>t &lt; t1</code>. <code>t == t1</code>.<br>
  669. Returns convertible to <code>bool</code>. See the <a href=
  670. "#ordering">Ordering Note</a>.</td>
  671. </tr>
  672. <tr>
  673. <td><code><a name="partially_ordered2">partially_ordered&lt;T,
  674. U&gt;</a></code><br>
  675. <code>partially_ordered2&lt;T, U&gt;</code></td>
  676. <td><code>bool operator&lt;=(const T&amp;, const U&amp;)</code><br>
  677. <code>bool operator&gt;=(const T&amp;, const U&amp;)</code><br>
  678. <code>bool operator&gt;(const U&amp;, const T&amp;)</code><br>
  679. <code>bool operator&lt;(const U&amp;, const T&amp;)</code><br>
  680. <code>bool operator&lt;=(const U&amp;, const T&amp;)</code><br>
  681. <code>bool operator&gt;=(const U&amp;, const T&amp;)</code></td>
  682. <td><code>t &lt; u</code>. <code>t &gt; u</code>. <code>t ==
  683. u</code>.<br>
  684. Returns convertible to <code>bool</code>. See the <a href=
  685. "#ordering">Ordering Note</a>.</td>
  686. </tr>
  687. </table>
  688. <h4><a name="ordering">Ordering</a> Note</h4>
  689. <p>The <code><a href=
  690. "#less_than_comparable1">less_than_comparable&lt;T&gt;</a></code> and
  691. <code><a href="#partially_ordered1">partially_ordered&lt;T&gt;</a></code>
  692. templates provide the same set of operations. However, the workings of
  693. <code><a href=
  694. "#less_than_comparable1">less_than_comparable&lt;T&gt;</a></code> assume
  695. that all values of type <code>T</code> can be placed in a total order. If
  696. that is not true (<i>e.g.</i> Not-a-Number values in IEEE floating point
  697. arithmetic), then <code><a href=
  698. "#partially_ordered1">partially_ordered&lt;T&gt;</a></code> should be
  699. used. The <code><a href=
  700. "#partially_ordered1">partially_ordered&lt;T&gt;</a></code> template can
  701. be used for a totally-ordered type, but it is not as efficient as
  702. <code><a href=
  703. "#less_than_comparable1">less_than_comparable&lt;T&gt;</a></code>. This
  704. rule also applies for <code><a href=
  705. "#less_than_comparable2">less_than_comparable&lt;T, U&gt;</a></code> and
  706. <code><a href="#partially_ordered2">partially_ordered&lt;T,
  707. U&gt;</a></code> with respect to the ordering of all <code>T</code> and
  708. <code>U</code> values, and for both versions of <code><a href=
  709. "#equivalent1">equivalent&lt;&gt;</a></code>. The solution for <code><a
  710. href="#equivalent1">equivalent&lt;&gt;</a></code> is to write a custom
  711. <code>operator==</code> for the target class.</p>
  712. <h4><a name="symmetry">Symmetry</a> Note</h4>
  713. <p>Before talking about symmetry, we need to talk about optimizations to
  714. understand the reasons for the different implementation styles of
  715. operators. Let's have a look at <code>operator+</code> for a class
  716. <code>T</code> as an example:</p>
  717. <pre>
  718. T operator+( const T&amp; lhs, const T&amp; rhs )
  719. {
  720. return T( lhs ) += rhs;
  721. }
  722. </pre>
  723. This would be a normal implementation of <code>operator+</code>, but it
  724. is not an efficient one. An unnamed local copy of <code>lhs</code> is
  725. created, <code>operator+=</code> is called on it and it is copied to the
  726. function return value (which is another unnamed object of type
  727. <code>T</code>). The standard doesn't generally allow the intermediate
  728. object to be optimized away:
  729. <blockquote>
  730. 3.7.2/2: Automatic storage duration<br>
  731. <br>
  732. If a named automatic object has initialization or a destructor with
  733. side effects, it shall not be destroyed before the end of its block,
  734. nor shall it be eliminated as an optimization even if it appears to be
  735. unused, except that a class object or its copy may be eliminated as
  736. specified in 12.8.
  737. </blockquote>
  738. The reference to 12.8 is important for us:
  739. <blockquote>
  740. 12.8/15: Copying class objects<br>
  741. ...<br>
  742. For a function with a class return type, if the expression in the
  743. return statement is the name of a local object, and the cv-unqualified
  744. type of the local object is the same as the function return type, an
  745. implementation is permitted to omit creating the temporary object to
  746. hold the function return value, even if the class copy constructor or
  747. destructor has side effects.
  748. </blockquote>
  749. This optimization is known as the named return value optimization (NRVO),
  750. which leads us to the following implementation for
  751. <code>operator+</code>:
  752. <pre>
  753. T operator+( const T&amp; lhs, const T&amp; rhs )
  754. {
  755. T nrv( lhs );
  756. nrv += rhs;
  757. return nrv;
  758. }
  759. </pre>
  760. Given this implementation, the compiler is allowed to remove the
  761. intermediate object. Sadly, not all compiler implement the NRVO, some
  762. even implement it in an incorrect way which makes it useless here.
  763. Without the NRVO, the NRVO-friendly code is no worse than the original
  764. code showed above, but there is another possible implementation, which
  765. has some very special properties:
  766. <pre>
  767. T operator+( T lhs, const T&amp; rhs )
  768. {
  769. return lhs += rhs;
  770. }
  771. </pre>
  772. The difference to the first implementation is that <code>lhs</code> is
  773. not taken as a constant reference used to create a copy; instead,
  774. <code>lhs</code> is a by-value parameter, thus it is already the copy
  775. needed. This allows another optimization (12.2/2) for some cases.
  776. Consider <code>a&nbsp;+&nbsp;b&nbsp;+&nbsp;c</code> where the result of
  777. <code>a&nbsp;+&nbsp;b</code> is not copied when used as <code>lhs</code>
  778. when adding <code>c</code>. This is more efficient than the original
  779. code, but not as efficient as a compiler using the NRVO. For most people,
  780. it is still preferable for compilers that don't implement the NRVO, but
  781. the <code>operator+</code> now has a different function signature. Also,
  782. the number of objects created differs for
  783. <code>(a&nbsp;+&nbsp;b&nbsp;)&nbsp;+&nbsp;c</code> and
  784. <code>a&nbsp;+&nbsp;(&nbsp;b&nbsp;+&nbsp;c&nbsp;)</code>. Most probably,
  785. this won't be a problem for you, but if your code relies on the function
  786. signature or a strict symmetric behaviour, you should set
  787. <code>BOOST_FORCE_SYMMETRIC_OPERATORS</code> in your user-config. This
  788. will force the NRVO-friendly implementation to be used even for compilers
  789. that don't implement the NRVO. <br>
  790. <br>
  791. <h3><a name="grpd_oprs">Grouped Arithmetic Operators</a></h3>
  792. <p>The following templates provide common groups of related operations.
  793. For example, since a type which is addable is usually also subractable,
  794. the <code><a href="#additive1">additive</a></code> template provides the
  795. combined operators of both. The grouped operator templates have an
  796. additional optional template parameter <code>B</code>, which is not
  797. shown, for the <a href="#chaining">base class chaining</a> technique.</p>
  798. <table cellpadding="5" border="1" align="center">
  799. <caption>
  800. Grouped Arithmetic Operator Template Classes
  801. </caption>
  802. <tr>
  803. <td colspan="2">
  804. <table align="center" border="1">
  805. <caption>
  806. <em>Key</em>
  807. </caption>
  808. <tr>
  809. <td><code>T</code>: primary operand type</td>
  810. <td><code>U</code>: alternate operand type</td>
  811. </tr>
  812. </table>
  813. </td>
  814. </tr>
  815. <tr>
  816. <th>Template</th>
  817. <th>Component Operator Templates</th>
  818. </tr>
  819. <tr>
  820. <td><code><a name=
  821. "totally_ordered1">totally_ordered&lt;T&gt;</a></code><br>
  822. <code>totally_ordered1&lt;T&gt;</code></td>
  823. <td>
  824. <ul>
  825. <li><code><a href=
  826. "#less_than_comparable1">less_than_comparable&lt;T&gt;</a></code></li>
  827. <li><code><a href=
  828. "#equality_comparable1">equality_comparable&lt;T&gt;</a></code></li>
  829. </ul>
  830. </td>
  831. </tr>
  832. <tr>
  833. <td><code><a name="totally_ordered2">totally_ordered&lt;T,
  834. U&gt;</a></code><br>
  835. <code>totally_ordered2&lt;T, U&gt;</code></td>
  836. <td>
  837. <ul>
  838. <li><code><a href=
  839. "#less_than_comparable2">less_than_comparable&lt;T,
  840. U&gt;</a></code></li>
  841. <li><code><a href=
  842. "#equality_comparable2">equality_comparable&lt;T,
  843. U&gt;</a></code></li>
  844. </ul>
  845. </td>
  846. </tr>
  847. <tr>
  848. <td><code><a name="additive1">additive&lt;T&gt;</a></code><br>
  849. <code>additive1&lt;T&gt;</code></td>
  850. <td>
  851. <ul>
  852. <li><code><a href="#addable1">addable&lt;T&gt;</a></code></li>
  853. <li><code><a href=
  854. "#subtractable1">subtractable&lt;T&gt;</a></code></li>
  855. </ul>
  856. </td>
  857. </tr>
  858. <tr>
  859. <td><code><a name="additive2">additive&lt;T, U&gt;</a></code><br>
  860. <code>additive2&lt;T, U&gt;</code></td>
  861. <td>
  862. <ul>
  863. <li><code><a href="#addable2">addable&lt;T, U&gt;</a></code></li>
  864. <li><code><a href="#subtractable2">subtractable&lt;T,
  865. U&gt;</a></code></li>
  866. </ul>
  867. </td>
  868. </tr>
  869. <tr>
  870. <td><code><a name=
  871. "multiplicative1">multiplicative&lt;T&gt;</a></code><br>
  872. <code>multiplicative1&lt;T&gt;</code></td>
  873. <td>
  874. <ul>
  875. <li><code><a href=
  876. "#multipliable1">multipliable&lt;T&gt;</a></code></li>
  877. <li><code><a href=
  878. "#dividable1">dividable&lt;T&gt;</a></code></li>
  879. </ul>
  880. </td>
  881. </tr>
  882. <tr>
  883. <td><code><a name="multiplicative2">multiplicative&lt;T,
  884. U&gt;</a></code><br>
  885. <code>multiplicative2&lt;T, U&gt;</code></td>
  886. <td>
  887. <ul>
  888. <li><code><a href="#multipliable2">multipliable&lt;T,
  889. U&gt;</a></code></li>
  890. <li><code><a href="#dividable2">dividable&lt;T,
  891. U&gt;</a></code></li>
  892. </ul>
  893. </td>
  894. </tr>
  895. <tr>
  896. <td><code><a name=
  897. "integer_multiplicative1">integer_multiplicative&lt;T&gt;</a></code><br>
  898. <code>integer_multiplicative1&lt;T&gt;</code></td>
  899. <td>
  900. <ul>
  901. <li><code><a href=
  902. "#multiplicative1">multiplicative&lt;T&gt;</a></code></li>
  903. <li><code><a href="#modable1">modable&lt;T&gt;</a></code></li>
  904. </ul>
  905. </td>
  906. </tr>
  907. <tr>
  908. <td><code><a name=
  909. "integer_multiplicative2">integer_multiplicative&lt;T,
  910. U&gt;</a></code><br>
  911. <code>integer_multiplicative2&lt;T, U&gt;</code></td>
  912. <td>
  913. <ul>
  914. <li><code><a href="#multiplicative2">multiplicative&lt;T,
  915. U&gt;</a></code></li>
  916. <li><code><a href="#modable2">modable&lt;T, U&gt;</a></code></li>
  917. </ul>
  918. </td>
  919. </tr>
  920. <tr>
  921. <td><code><a name="arithmetic1">arithmetic&lt;T&gt;</a></code><br>
  922. <code>arithmetic1&lt;T&gt;</code></td>
  923. <td>
  924. <ul>
  925. <li><code><a href="#additive1">additive&lt;T&gt;</a></code></li>
  926. <li><code><a href=
  927. "#multiplicative1">multiplicative&lt;T&gt;</a></code></li>
  928. </ul>
  929. </td>
  930. </tr>
  931. <tr>
  932. <td><code><a name="arithmetic2">arithmetic&lt;T, U&gt;</a></code><br>
  933. <code>arithmetic2&lt;T, U&gt;</code></td>
  934. <td>
  935. <ul>
  936. <li><code><a href="#additive2">additive&lt;T,
  937. U&gt;</a></code></li>
  938. <li><code><a href="#multiplicative2">multiplicative&lt;T,
  939. U&gt;</a></code></li>
  940. </ul>
  941. </td>
  942. </tr>
  943. <tr>
  944. <td><code><a name=
  945. "integer_arithmetic1">integer_arithmetic&lt;T&gt;</a></code><br>
  946. <code>integer_arithmetic1&lt;T&gt;</code></td>
  947. <td>
  948. <ul>
  949. <li><code><a href="#additive1">additive&lt;T&gt;</a></code></li>
  950. <li><code><a href=
  951. "#integer_multiplicative1">integer_multiplicative&lt;T&gt;</a></code></li>
  952. </ul>
  953. </td>
  954. </tr>
  955. <tr>
  956. <td><code><a name="integer_arithmetic2">integer_arithmetic&lt;T,
  957. U&gt;</a></code><br>
  958. <code>integer_arithmetic2&lt;T, U&gt;</code></td>
  959. <td>
  960. <ul>
  961. <li><code><a href="#additive2">additive&lt;T,
  962. U&gt;</a></code></li>
  963. <li><code><a href=
  964. "#integer_multiplicative2">integer_multiplicative&lt;T,
  965. U&gt;</a></code></li>
  966. </ul>
  967. </td>
  968. </tr>
  969. <tr>
  970. <td><code><a name="bitwise1">bitwise&lt;T&gt;</a></code><br>
  971. <code>bitwise1&lt;T&gt;</code></td>
  972. <td>
  973. <ul>
  974. <li><code><a href="#xorable1">xorable&lt;T&gt;</a></code></li>
  975. <li><code><a href="#andable1">andable&lt;T&gt;</a></code></li>
  976. <li><code><a href="#orable1">orable&lt;T&gt;</a></code></li>
  977. </ul>
  978. </td>
  979. </tr>
  980. <tr>
  981. <td><code><a name="bitwise2">bitwise&lt;T, U&gt;</a></code><br>
  982. <code>bitwise2&lt;T, U&gt;</code></td>
  983. <td>
  984. <ul>
  985. <li><code><a href="#xorable2">xorable&lt;T, U&gt;</a></code></li>
  986. <li><code><a href="#andable2">andable&lt;T, U&gt;</a></code></li>
  987. <li><code><a href="#orable2">orable&lt;T, U&gt;</a></code></li>
  988. </ul>
  989. </td>
  990. </tr>
  991. <tr>
  992. <td><code><a name=
  993. "unit_steppable">unit_steppable&lt;T&gt;</a></code></td>
  994. <td>
  995. <ul>
  996. <li><code><a href=
  997. "#incrementable">incrementable&lt;T&gt;</a></code></li>
  998. <li><code><a href=
  999. "#decrementable">decrementable&lt;T&gt;</a></code></li>
  1000. </ul>
  1001. </td>
  1002. </tr>
  1003. <tr>
  1004. <td><code><a name="shiftable1">shiftable&lt;T&gt;</a></code><br>
  1005. <code>shiftable1&lt;T&gt;</code></td>
  1006. <td>
  1007. <ul>
  1008. <li><code><a href=
  1009. "#left_shiftable1">left_shiftable&lt;T&gt;</a></code></li>
  1010. <li><code><a href=
  1011. "#right_shiftable1">right_shiftable&lt;T&gt;</a></code></li>
  1012. </ul>
  1013. </td>
  1014. </tr>
  1015. <tr>
  1016. <td><code><a name="shiftable2">shiftable&lt;T, U&gt;</a></code><br>
  1017. <code>shiftable2&lt;T, U&gt;</code></td>
  1018. <td>
  1019. <ul>
  1020. <li><code><a href="#left_shiftable2">left_shiftable&lt;T,
  1021. U&gt;</a></code></li>
  1022. <li><code><a href="#right_shiftable2">right_shiftable&lt;T,
  1023. U&gt;</a></code></li>
  1024. </ul>
  1025. </td>
  1026. </tr>
  1027. <tr>
  1028. <td><code><a name=
  1029. "ring_operators1">ring_operators&lt;T&gt;</a></code><br>
  1030. <code>ring_operators1&lt;T&gt;</code></td>
  1031. <td>
  1032. <ul>
  1033. <li><code><a href="#additive1">additive&lt;T&gt;</a></code></li>
  1034. <li><code><a href=
  1035. "#multipliable1">multipliable&lt;T&gt;</a></code></li>
  1036. </ul>
  1037. </td>
  1038. </tr>
  1039. <tr>
  1040. <td><code><a name="ring_operators2">ring_operators&lt;T,
  1041. U&gt;</a></code><br>
  1042. <code>ring_operators2&lt;T, U&gt;</code></td>
  1043. <td>
  1044. <ul>
  1045. <li><code><a href="#additive2">additive&lt;T,
  1046. U&gt;</a></code></li>
  1047. <li><code><a href="#subtractable2_left">subtractable2_left&lt;T,
  1048. U&gt;</a></code></li>
  1049. <li><code><a href="#multipliable2">multipliable&lt;T,
  1050. U&gt;</a></code></li>
  1051. </ul>
  1052. </td>
  1053. </tr>
  1054. <tr>
  1055. <td><code><a name=
  1056. "ordered_ring_operators1">ordered_ring_operators&lt;T&gt;</a></code><br>
  1057. <code>ordered_ring_operators1&lt;T&gt;</code></td>
  1058. <td>
  1059. <ul>
  1060. <li><code><a href=
  1061. "#ring_operators1">ring_operators&lt;T&gt;</a></code></li>
  1062. <li><code><a href=
  1063. "#totally_ordered1">totally_ordered&lt;T&gt;</a></code></li>
  1064. </ul>
  1065. </td>
  1066. </tr>
  1067. <tr>
  1068. <td><code><a name=
  1069. "ordered_ring_operators2">ordered_ring_operators&lt;T,
  1070. U&gt;</a></code><br>
  1071. <code>ordered_ring_operators2&lt;T, U&gt;</code></td>
  1072. <td>
  1073. <ul>
  1074. <li><code><a href="#ring_operators2">ring_operators&lt;T,
  1075. U&gt;</a></code></li>
  1076. <li><code><a href="#totally_ordered2">totally_ordered&lt;T,
  1077. U&gt;</a></code></li>
  1078. </ul>
  1079. </td>
  1080. </tr>
  1081. <tr>
  1082. <td><code><a name=
  1083. "field_operators1">field_operators&lt;T&gt;</a></code><br>
  1084. <code>field_operators1&lt;T&gt;</code></td>
  1085. <td>
  1086. <ul>
  1087. <li><code><a href=
  1088. "#ring_operators1">ring_operators&lt;T&gt;</a></code></li>
  1089. <li><code><a href=
  1090. "#dividable1">dividable&lt;T&gt;</a></code></li>
  1091. </ul>
  1092. </td>
  1093. </tr>
  1094. <tr>
  1095. <td><code><a name="field_operators2">field_operators&lt;T,
  1096. U&gt;</a></code><br>
  1097. <code>field_operators2&lt;T, U&gt;</code></td>
  1098. <td>
  1099. <ul>
  1100. <li><code><a href="#ring_operators2">ring_operators&lt;T,
  1101. U&gt;</a></code></li>
  1102. <li><code><a href="#dividable2">dividable&lt;T,
  1103. U&gt;</a></code></li>
  1104. <li><code><a href="#dividable2_left">dividable2_left&lt;T,
  1105. U&gt;</a></code></li>
  1106. </ul>
  1107. </td>
  1108. </tr>
  1109. <tr>
  1110. <td><code><a name=
  1111. "ordered_field_operators1">ordered_field_operators&lt;T&gt;</a></code><br>
  1112. <code>ordered_field_operators1&lt;T&gt;</code></td>
  1113. <td>
  1114. <ul>
  1115. <li><code><a href=
  1116. "#field_operators1">field_operators&lt;T&gt;</a></code></li>
  1117. <li><code><a href=
  1118. "#totally_ordered1">totally_ordered&lt;T&gt;</a></code></li>
  1119. </ul>
  1120. </td>
  1121. </tr>
  1122. <tr>
  1123. <td><code><a name=
  1124. "ordered_field_operators2">ordered_field_operators&lt;T,
  1125. U&gt;</a></code><br>
  1126. <code>ordered_field_operators2&lt;T, U&gt;</code></td>
  1127. <td>
  1128. <ul>
  1129. <li><code><a href="#field_operators2">field_operators&lt;T,
  1130. U&gt;</a></code></li>
  1131. <li><code><a href="#totally_ordered2">totally_ordered&lt;T,
  1132. U&gt;</a></code></li>
  1133. </ul>
  1134. </td>
  1135. </tr>
  1136. <tr>
  1137. <td><code><a name=
  1138. "euclidean_ring_operators1">euclidean_ring_operators&lt;T&gt;</a></code><br>
  1139. <code>euclidean_ring_operators1&lt;T&gt;</code></td>
  1140. <td>
  1141. <ul>
  1142. <li><code><a href=
  1143. "#ring_operators1">ring_operators&lt;T&gt;</a></code></li>
  1144. <li><code><a href=
  1145. "#dividable1">dividable&lt;T&gt;</a></code></li>
  1146. <li><code><a href="#modable1">modable&lt;T&gt;</a></code></li>
  1147. </ul>
  1148. </td>
  1149. </tr>
  1150. <tr>
  1151. <td><code><a name=
  1152. "euclidean_ring_operators2">euclidean_ring_operators&lt;T,
  1153. U&gt;</a></code><br>
  1154. <code>euclidean_ring_operators2&lt;T, U&gt;</code></td>
  1155. <td>
  1156. <ul>
  1157. <li><code><a href="#ring_operators2">ring_operators&lt;T,
  1158. U&gt;</a></code></li>
  1159. <li><code><a href="#dividable2">dividable&lt;T,
  1160. U&gt;</a></code></li>
  1161. <li><code><a href="#dividable2_left">dividable2_left&lt;T,
  1162. U&gt;</a></code></li>
  1163. <li><code><a href="#modable2">modable&lt;T, U&gt;</a></code></li>
  1164. <li><code><a href="#modable2_left">modable2_left&lt;T,
  1165. U&gt;</a></code></li>
  1166. </ul>
  1167. </td>
  1168. </tr>
  1169. <tr>
  1170. <td><code><a name=
  1171. "ordered_euclidean_ring_operators1">ordered_euclidean_ring_operators&lt;T&gt;</a></code><br>
  1172. <code>ordered_euclidean_ring_operators1&lt;T&gt;</code></td>
  1173. <td>
  1174. <ul>
  1175. <li><code><a href=
  1176. "#euclidean_ring_operators1">euclidean_ring_operators&lt;T&gt;</a></code></li>
  1177. <li><code><a href=
  1178. "#totally_ordered1">totally_ordered&lt;T&gt;</a></code></li>
  1179. </ul>
  1180. </td>
  1181. </tr>
  1182. <tr>
  1183. <td><code><a name=
  1184. "ordered_euclidean_ring_operators2">ordered_euclidean_ring_operators&lt;T,
  1185. U&gt;</a></code><br>
  1186. <code>ordered_euclidean_ring_operators2&lt;T, U&gt;</code></td>
  1187. <td>
  1188. <ul>
  1189. <li><code><a href=
  1190. "#euclidean_ring_operators2">euclidean_ring_operators&lt;T,
  1191. U&gt;</a></code></li>
  1192. <li><code><a href="#totally_ordered2">totally_ordered&lt;T,
  1193. U&gt;</a></code></li>
  1194. </ul>
  1195. </td>
  1196. </tr>
  1197. </table>
  1198. <h4>Spelling: euclidean vs. euclidian</h4>
  1199. <p>Older versions of the Boost.Operators library used
  1200. &quot;<code>euclidian</code>&quot;, but it was pointed out that
  1201. &quot;<code>euclidean</code>&quot; is the more common spelling.
  1202. To be compatible with older version, the library now supports
  1203. both spellings.
  1204. </p>
  1205. <h3><a name="ex_oprs">Example</a> Templates</h3>
  1206. <p>The arithmetic operator class templates <code><a href=
  1207. "#operators1">operators&lt;&gt;</a></code> and <code><a href=
  1208. "#operators2">operators2&lt;&gt;</a></code> are examples of
  1209. non-extensible operator grouping classes. These legacy class templates,
  1210. from previous versions of the header, cannot be used for <a href=
  1211. "#chaining">base class chaining</a>.</p>
  1212. <table cellpadding="5" border="1" align="center">
  1213. <caption>
  1214. Final Arithmetic Operator Template Classes
  1215. </caption>
  1216. <tr>
  1217. <td colspan="2">
  1218. <table align="center" border="1">
  1219. <caption>
  1220. <em>Key</em>
  1221. </caption>
  1222. <tr>
  1223. <td><code>T</code>: primary operand type</td>
  1224. <td><code>U</code>: alternate operand type</td>
  1225. </tr>
  1226. </table>
  1227. </td>
  1228. </tr>
  1229. <tr>
  1230. <th>Template</th>
  1231. <th>Component Operator Templates</th>
  1232. </tr>
  1233. <tr>
  1234. <td><code><a name="operators1">operators&lt;T&gt;</a></code></td>
  1235. <td>
  1236. <ul>
  1237. <li><code><a href=
  1238. "#totally_ordered1">totally_ordered&lt;T&gt;</a></code></li>
  1239. <li><code><a href=
  1240. "#integer_arithmetic1">integer_arithmetic&lt;T&gt;</a></code></li>
  1241. <li><code><a href="#bitwise1">bitwise&lt;T&gt;</a></code></li>
  1242. <li><code><a href=
  1243. "#unit_steppable">unit_steppable&lt;T&gt;</a></code></li>
  1244. </ul>
  1245. </td>
  1246. </tr>
  1247. <tr>
  1248. <td><code><a name="operators2">operators&lt;T, U&gt;</a></code><br>
  1249. <code>operators2&lt;T, U&gt;</code></td>
  1250. <td>
  1251. <ul>
  1252. <li><code><a href="#totally_ordered2">totally_ordered&lt;T,
  1253. U&gt;</a></code></li>
  1254. <li><code><a href="#integer_arithmetic2">integer_arithmetic&lt;T,
  1255. U&gt;</a></code></li>
  1256. <li><code><a href="#bitwise2">bitwise&lt;T, U&gt;</a></code></li>
  1257. </ul>
  1258. </td>
  1259. </tr>
  1260. </table>
  1261. <h3><a name="a_demo">Arithmetic Operators Demonstration</a> and Test
  1262. Program</h3>
  1263. <p>The <cite><a href="test/operators_test.cpp">operators_test.cpp</a></cite>
  1264. program demonstrates the use of the arithmetic operator templates, and
  1265. can also be used to verify correct operation. Check the compiler status
  1266. report for the test results with selected platforms.</p>
  1267. <h2><a name="deref">Dereference</a> Operators and Iterator Helpers</h2>
  1268. <p>The <a href="#iterator">iterator helper</a> templates ease the task of
  1269. creating a custom iterator. Similar to arithmetic types, a complete
  1270. iterator has many operators that are "redundant" and can be implemented
  1271. in terms of the core set of operators.</p>
  1272. <p>The <a href="#dereference">dereference operators</a> were motivated by
  1273. the <a href="#iterator">iterator helpers</a>, but are often useful in
  1274. non-iterator contexts as well. Many of the redundant iterator operators
  1275. are also arithmetic operators, so the iterator helper classes borrow many
  1276. of the operators defined above. In fact, only two new operators need to
  1277. be defined (the pointer-to-member <code>operator-&gt;</code> and the
  1278. subscript <code>operator[]</code>)!</p>
  1279. <p>The requirements for the types used to instantiate the dereference
  1280. operators are specified in terms of expressions which must be valid and
  1281. their return type. The composite operator templates list their component
  1282. templates, which the instantiating type must support, and possibly other
  1283. requirements.</p>
  1284. <h3><a name="dereference">Dereference</a> Operators</h3>
  1285. <p>All the dereference operator templates in this table accept an
  1286. optional template parameter (not shown) to be used for <a href=
  1287. "#chaining">base class chaining</a>.</p>
  1288. <table cellpadding="5" border="1" align="center">
  1289. <caption>
  1290. Dereference Operator Template Classes
  1291. </caption>
  1292. <tr>
  1293. <td colspan="3">
  1294. <table align="center" border="1">
  1295. <caption>
  1296. <em>Key</em>
  1297. </caption>
  1298. <tr>
  1299. <td><code>T</code>: operand type</td>
  1300. <td><code>P</code>: <code>pointer</code> type</td>
  1301. </tr>
  1302. <tr>
  1303. <td><code>D</code>: <code>difference_type</code></td>
  1304. <td><code>R</code>: <code>reference</code> type</td>
  1305. </tr>
  1306. <tr>
  1307. <td><code>i</code>: object of type <code>T</code> (an
  1308. iterator)</td>
  1309. <td><code>n</code>: object of type <code>D</code> (an
  1310. index)</td>
  1311. </tr>
  1312. </table>
  1313. </td>
  1314. </tr>
  1315. <tr>
  1316. <th>Template</th>
  1317. <th>Supplied Operations</th>
  1318. <th>Requirements</th>
  1319. </tr>
  1320. <tr>
  1321. <td><code><a name="dereferenceable">dereferenceable&lt;T,
  1322. P&gt;</a></code></td>
  1323. <td><code>P operator-&gt;() const</code></td>
  1324. <td><code>*i</code>. Address of the returned value convertible
  1325. to <code>P</code>.</td>
  1326. </tr>
  1327. <tr>
  1328. <td><code><a name="indexable">indexable&lt;T, D,
  1329. R&gt;</a></code></td>
  1330. <td><code>R operator[](D n) const</code></td>
  1331. <td><code>*(i&nbsp;+&nbsp;n)</code>. Return of type
  1332. <code>R</code>.</td>
  1333. </tr>
  1334. </table>
  1335. <h3><a name="grpd_iter_oprs">Grouped Iterator Operators</a></h3>
  1336. <p>There are five iterator operator class templates, each for a different
  1337. category of iterator. The following table shows the operator groups for
  1338. any category that a custom iterator could define. These class templates
  1339. have an additional optional template parameter <code>B</code>, which is
  1340. not shown, to support <a href="#chaining">base class chaining</a>.</p>
  1341. <table cellpadding="5" border="1" align="center">
  1342. <caption>
  1343. Iterator Operator Class Templates
  1344. </caption>
  1345. <tr>
  1346. <td colspan="2">
  1347. <table align="center" border="1">
  1348. <caption>
  1349. <em>Key</em>
  1350. </caption>
  1351. <tr>
  1352. <td><code>T</code>: operand type</td>
  1353. <td><code>P</code>: <code>pointer</code> type</td>
  1354. </tr>
  1355. <tr>
  1356. <td><code>D</code>: <code>difference_type</code></td>
  1357. <td><code>R</code>: <code>reference</code> type</td>
  1358. </tr>
  1359. <tr>
  1360. <td><code>V</code>: <code>value_type</code></td>
  1361. <td>
  1362. </td>
  1363. </tr>
  1364. </table>
  1365. </td>
  1366. </tr>
  1367. <tr>
  1368. <th>Template</th>
  1369. <th>Component Operator Templates</th>
  1370. </tr>
  1371. <tr>
  1372. <td><code><a name="input_iteratable">input_iteratable&lt;T,
  1373. P&gt;</a></code></td>
  1374. <td>
  1375. <ul>
  1376. <li><code><a href=
  1377. "#equality_comparable1">equality_comparable&lt;T&gt;</a></code></li>
  1378. <li><code><a href=
  1379. "#incrementable">incrementable&lt;T&gt;</a></code></li>
  1380. <li><code><a href="#dereferenceable">dereferenceable&lt;T,
  1381. P&gt;</a></code></li>
  1382. </ul>
  1383. </td>
  1384. </tr>
  1385. <tr>
  1386. <td><code><a name=
  1387. "output_iteratable">output_iteratable&lt;T&gt;</a></code></td>
  1388. <td>
  1389. <ul>
  1390. <li><code><a href=
  1391. "#incrementable">incrementable&lt;T&gt;</a></code></li>
  1392. </ul>
  1393. </td>
  1394. </tr>
  1395. <tr>
  1396. <td><code><a name="forward_iteratable">forward_iteratable&lt;T,
  1397. P&gt;</a></code></td>
  1398. <td>
  1399. <ul>
  1400. <li><code><a href="#input_iteratable">input_iteratable&lt;T,
  1401. P&gt;</a></code></li>
  1402. </ul>
  1403. </td>
  1404. </tr>
  1405. <tr>
  1406. <td><code><a name=
  1407. "bidirectional_iteratable">bidirectional_iteratable&lt;T,
  1408. P&gt;</a></code></td>
  1409. <td>
  1410. <ul>
  1411. <li><code><a href="#forward_iteratable">forward_iteratable&lt;T,
  1412. P&gt;</a></code></li>
  1413. <li><code><a href=
  1414. "#decrementable">decrementable&lt;T&gt;</a></code></li>
  1415. </ul>
  1416. </td>
  1417. </tr>
  1418. <tr>
  1419. <td><code><a name=
  1420. "random_access_iteratable">random_access_iteratable&lt;T, P, D,
  1421. R&gt;</a></code></td>
  1422. <td>
  1423. <ul>
  1424. <li><code><a href=
  1425. "#bidirectional_iteratable">bidirectional_iteratable&lt;T,
  1426. P&gt;</a></code></li>
  1427. <li><code><a href=
  1428. "#totally_ordered1">totally_ordered&lt;T&gt;</a></code></li>
  1429. <li><code><a href="#additive2">additive&lt;T,
  1430. D&gt;</a></code></li>
  1431. <li><code><a href="#indexable">indexable&lt;T, D,
  1432. R&gt;</a></code></li>
  1433. </ul>
  1434. </td>
  1435. </tr>
  1436. </table>
  1437. <h3><a name="iterator">Iterator</a> Helpers</h3>
  1438. <p>There are also five iterator helper class templates, each
  1439. corresponding to a different iterator category. These classes cannot be
  1440. used for <a href="#chaining">base class chaining</a>. The following
  1441. summaries show that these class templates supply both the iterator
  1442. operators from the <a href="#grpd_iter_oprs">iterator operator class
  1443. templates</a> and the iterator typedef's required by the C++ standard
  1444. (<code>iterator_category</code>, <code>value_type</code>,
  1445. <i>etc.</i>).</p>
  1446. <table cellpadding="5" border="1" align="center">
  1447. <caption>
  1448. Iterator Helper Class Templates
  1449. </caption>
  1450. <tr>
  1451. <td colspan="2">
  1452. <table align="center" border="1">
  1453. <caption>
  1454. <em>Key</em>
  1455. </caption>
  1456. <tr>
  1457. <td><code>T</code>: operand type</td>
  1458. <td><code>P</code>: <code>pointer</code> type</td>
  1459. </tr>
  1460. <tr>
  1461. <td><code>D</code>: <code>difference_type</code></td>
  1462. <td><code>R</code>: <code>reference</code> type</td>
  1463. </tr>
  1464. <tr>
  1465. <td><code>V</code>: <code>value_type</code></td>
  1466. <td><code>x1, x2</code>: objects of type <code>T</code></td>
  1467. </tr>
  1468. </table>
  1469. </td>
  1470. </tr>
  1471. <tr>
  1472. <th>Template</th>
  1473. <th>Operations &amp; Requirements</th>
  1474. </tr>
  1475. <tr valign="baseline">
  1476. <td><code><a name="input_iterator_helper">input_iterator_helper&lt;T,
  1477. V, D, P, R&gt;</a></code></td>
  1478. <td>
  1479. Supports the operations and has the requirements of
  1480. <ul>
  1481. <li><code><a href="#input_iteratable">input_iteratable&lt;T,
  1482. P&gt;</a></code></li>
  1483. </ul>
  1484. </td>
  1485. </tr>
  1486. <tr valign="baseline">
  1487. <td><code><a name=
  1488. "output_iterator_helper">output_iterator_helper&lt;T&gt;</a></code></td>
  1489. <td>
  1490. Supports the operations and has the requirements of
  1491. <ul>
  1492. <li><code><a href=
  1493. "#output_iteratable">output_iteratable&lt;T&gt;</a></code></li>
  1494. </ul>
  1495. See also [<a href="#1">1</a>], [<a href="#2">2</a>].
  1496. </td>
  1497. </tr>
  1498. <tr valign="baseline">
  1499. <td><code><a name=
  1500. "forward_iterator_helper">forward_iterator_helper&lt;T, V, D, P,
  1501. R&gt;</a></code></td>
  1502. <td>
  1503. Supports the operations and has the requirements of
  1504. <ul>
  1505. <li><code><a href="#forward_iteratable">forward_iteratable&lt;T,
  1506. P&gt;</a></code></li>
  1507. </ul>
  1508. </td>
  1509. </tr>
  1510. <tr valign="baseline">
  1511. <td><code><a name=
  1512. "bidirectional_iterator_helper">bidirectional_iterator_helper&lt;T,
  1513. V, D, P, R&gt;</a></code></td>
  1514. <td>
  1515. Supports the operations and has the requirements of
  1516. <ul>
  1517. <li><code><a href=
  1518. "#bidirectional_iteratable">bidirectional_iteratable&lt;T,
  1519. P&gt;</a></code></li>
  1520. </ul>
  1521. </td>
  1522. </tr>
  1523. <tr valign="baseline">
  1524. <td><code><a name=
  1525. "random_access_iterator_helper">random_access_iterator_helper&lt;T,
  1526. V, D, P, R&gt;</a></code></td>
  1527. <td>
  1528. Supports the operations and has the requirements of
  1529. <ul>
  1530. <li><code><a href=
  1531. "#random_access_iteratable">random_access_iteratable&lt;T, P, D,
  1532. R&gt;</a></code></li>
  1533. </ul>
  1534. To satisfy <cite><a href=
  1535. "http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator</a></cite>,
  1536. <code>x1 - x2</code> with return convertible to <code>D</code> is
  1537. also required.
  1538. </td>
  1539. </tr>
  1540. </table>
  1541. <h4><a name="iterator_helpers_notes">Iterator Helper Notes</a></h4>
  1542. <p><a name="1">[1]</a> Unlike other iterator helpers templates,
  1543. <code>output_iterator_helper</code> takes only one template parameter -
  1544. the type of its target class. Although to some it might seem like an
  1545. unnecessary restriction, the standard requires
  1546. <code>difference_type</code> and <code>value_type</code> of any output
  1547. iterator to be <code>void</code> (24.3.1 [lib.iterator.traits]), and
  1548. <code>output_iterator_helper</code> template respects this requirement.
  1549. Also, output iterators in the standard have void <code>pointer</code> and
  1550. <code>reference</code> types, so the <code>output_iterator_helper</code>
  1551. does the same.</p>
  1552. <p><a name="2">[2]</a> As self-proxying is the easiest and most common
  1553. way to implement output iterators (see, for example, insert [24.4.2] and
  1554. stream iterators [24.5] in the standard library),
  1555. <code>output_iterator_helper</code> supports the idiom by defining
  1556. <code>operator*</code> and <code>operator++</code> member functions which
  1557. just return a non-const reference to the iterator itself. Support for
  1558. self-proxying allows us, in many cases, to reduce the task of writing an
  1559. output iterator to writing just two member functions - an appropriate
  1560. constructor and a copy-assignment operator. For example, here is a
  1561. possible implementation of <code><a href=
  1562. "../iterator/doc/function_output_iterator.html">boost::function_output_iterator</a></code>
  1563. adaptor:</p>
  1564. <pre>
  1565. template&lt;class UnaryFunction&gt;
  1566. struct function_output_iterator
  1567. : boost::output_iterator_helper&lt; function_output_iterator&lt;UnaryFunction&gt; &gt;
  1568. {
  1569. explicit function_output_iterator(UnaryFunction const&amp; f = UnaryFunction())
  1570. : func(f) {}
  1571. template&lt;typename T&gt;
  1572. function_output_iterator&amp; operator=(T const&amp; value)
  1573. {
  1574. this-&gt;func(value);
  1575. return *this;
  1576. }
  1577. private:
  1578. UnaryFunction func;
  1579. };
  1580. </pre>
  1581. <p>Note that support for self-proxying does not prevent you from using
  1582. <code>output_iterator_helper</code> to ease any other, different kind of
  1583. output iterator's implementation. If
  1584. <code>output_iterator_helper</code>'s target type provides its own
  1585. definition of <code>operator*</code> or/and <code>operator++</code>, then
  1586. these operators will get used and the ones supplied by
  1587. <code>output_iterator_helper</code> will never be instantiated.</p>
  1588. <h3><a name="i_demo">Iterator Demonstration</a> and Test Program</h3>
  1589. <p>The <cite><a href="test/iterators_test.cpp">iterators_test.cpp</a></cite>
  1590. program demonstrates the use of the iterator templates, and can also be
  1591. used to verify correct operation. The following is the custom iterator
  1592. defined in the test program. It demonstrates a correct (though trivial)
  1593. implementation of the core operations that must be defined in order for
  1594. the iterator helpers to "fill in" the rest of the iterator
  1595. operations.</p>
  1596. <blockquote>
  1597. <pre>
  1598. template &lt;class T, class R, class P&gt;
  1599. struct test_iter
  1600. : public boost::random_access_iterator_helper&lt;
  1601. test_iter&lt;T,R,P&gt;, T, std::ptrdiff_t, P, R&gt;
  1602. {
  1603. typedef test_iter self;
  1604. typedef R Reference;
  1605. typedef std::ptrdiff_t Distance;
  1606. public:
  1607. explicit test_iter(T* i =0);
  1608. test_iter(const self&amp; x);
  1609. self&amp; operator=(const self&amp; x);
  1610. Reference operator*() const;
  1611. self&amp; operator++();
  1612. self&amp; operator--();
  1613. self&amp; operator+=(Distance n);
  1614. self&amp; operator-=(Distance n);
  1615. bool operator==(const self&amp; x) const;
  1616. bool operator&lt;(const self&amp; x) const;
  1617. friend Distance operator-(const self&amp; x, const self&amp; y);
  1618. };
  1619. </pre>
  1620. </blockquote>
  1621. <p>Check the <a href="http://www.boost.org/development/testing.html">compiler status
  1622. report</a> for the test results with selected platforms.</p>
  1623. <hr>
  1624. <h2><a name="contributors">Contributors</a></h2>
  1625. <dl>
  1626. <dt><a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a></dt>
  1627. <dd>Started the library and contributed the arithmetic operators in
  1628. <cite><a href=
  1629. "../../boost/operators.hpp">boost/operators.hpp</a></cite>.</dd>
  1630. <dt><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</a></dt>
  1631. <dd>Contributed the <a href="#deref">dereference operators and iterator
  1632. helpers</a> in <cite><a href=
  1633. "../../boost/operators.hpp">boost/operators.hpp</a></cite>. Also
  1634. contributed <cite><a href=
  1635. "iterators_test.cpp">iterators_test.cpp</a></cite>.</dd>
  1636. <dt><a href="http://www.boost.org/people/aleksey_gurtovoy.htm">Aleksey
  1637. Gurtovoy</a></dt>
  1638. <dd>Contributed the code to support <a href="#chaining">base class
  1639. chaining</a> while remaining backward-compatible with old versions of
  1640. the library.</dd>
  1641. <dt><a href="http://www.boost.org/people/beman_dawes.html">Beman Dawes</a></dt>
  1642. <dd>Contributed <cite><a href=
  1643. "test/operators_test.cpp">operators_test.cpp</a></cite>.</dd>
  1644. <dt><a href="http://www.boost.org/people/daryle_walker.html">Daryle Walker</a></dt>
  1645. <dd>Contributed classes for the shift operators, equivalence, partial
  1646. ordering, and arithmetic conversions. Added the grouped operator
  1647. classes. Added helper classes for input and output iterators.</dd>
  1648. <dt>Helmut Zeisel</dt>
  1649. <dd>Contributed the 'left' operators and added some grouped operator
  1650. classes.</dd>
  1651. <dt>Daniel Frey</dt>
  1652. <dd>Contributed the NRVO-friendly and symmetric implementation of
  1653. arithmetic operators.</dd>
  1654. </dl>
  1655. <h2>Note for Users of <a name="old_lib_note">Older Versions</a></h2>
  1656. <p>The <a href="#chaining">changes in the library interface and
  1657. recommended usage</a> were motivated by some practical issues described
  1658. below. The new version of the library is still backward-compatible with
  1659. the former one (so you're not <em>forced</em> change any existing code),
  1660. but the old usage is deprecated. Though it was arguably simpler and more
  1661. intuitive than using <a href="#chaining">base class chaining</a>, it has
  1662. been discovered that the old practice of deriving from multiple operator
  1663. templates can cause the resulting classes to be much larger than they
  1664. should be. Most modern C++ compilers significantly bloat the size of
  1665. classes derived from multiple empty base classes, even though the base
  1666. classes themselves have no state. For instance, the size of
  1667. <code>point&lt;int&gt;</code> from the <a href="#example">example</a>
  1668. above was 12-24 bytes on various compilers for the Win32 platform,
  1669. instead of the expected 8 bytes.</p>
  1670. <p>Strictly speaking, it was not the library's fault--the language rules
  1671. allow the compiler to apply the empty base class optimization in that
  1672. situation. In principle an arbitrary number of empty base classes can be
  1673. allocated at the same offset, provided that none of them have a common
  1674. ancestor (see section 10.5 [class.derived] paragraph 5 of the standard).
  1675. But the language definition also doesn't <em>require</em> implementations
  1676. to do the optimization, and few if any of today's compilers implement it
  1677. when multiple inheritance is involved. What's worse, it is very unlikely
  1678. that implementors will adopt it as a future enhancement to existing
  1679. compilers, because it would break binary compatibility between code
  1680. generated by two different versions of the same compiler. As Matt Austern
  1681. said, "One of the few times when you have the freedom to do this sort of
  1682. thing is when you're targeting a new architecture...". On the other hand,
  1683. many common compilers will use the empty base optimization for single
  1684. inheritance hierarchies.</p>
  1685. <p>Given the importance of the issue for the users of the library (which
  1686. aims to be useful for writing light-weight classes like
  1687. <code>MyInt</code> or <code>point&lt;&gt;</code>), and the forces
  1688. described above, we decided to change the library interface so that the
  1689. object size bloat could be eliminated even on compilers that support only
  1690. the simplest form of the empty base class optimization. The current
  1691. library interface is the result of those changes. Though the new usage is
  1692. a bit more complicated than the old one, we think it's worth it to make
  1693. the library more useful in real world. Alexy Gurtovoy contributed the
  1694. code which supports the new usage idiom while allowing the library remain
  1695. backward-compatible.</p>
  1696. <hr>
  1697. <p>Revised: 7 Aug 2008</p>
  1698. <p>Copyright &copy; Beman Dawes, David Abrahams, 1999-2001.</p>
  1699. <p>Copyright &copy; Daniel Frey, 2002-2009.</p>
  1700. <p>Use, modification, and distribution is subject to the Boost Software
  1701. License, Version 1.0. (See accompanying file
  1702. <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy at
  1703. <a href="http://www.boost.org/LICENSE_1_0.txt">
  1704. www.boost.org/LICENSE_1_0.txt</a>)</p>
  1705. </body>
  1706. </html>