1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Inplace and infix operators</title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Icl"> 8<link rel="up" href="../implementation.html" title="Implementation"> 9<link rel="prev" href="complexity.html" title="Complexity"> 10<link rel="next" href="../function_reference.html" title="Function Reference"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="complexity.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../implementation.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../function_reference.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_icl.implementation.inplace_and_infix_operators"></a><a class="link" href="inplace_and_infix_operators.html" title="Inplace and infix operators">Inplace 28 and infix operators</a> 29</h3></div></div></div> 30<p> 31 For the major operations <span class="emphasis"><em>addition, subtraction, intersection</em></span> 32 of <span class="bold"><strong>icl</strong></span> containers and for <span class="emphasis"><em>symmetric 33 difference</em></span> inplace <code class="computeroutput"><span class="keyword">operator</span></code>s 34 <code class="computeroutput"><span class="special">+=</span> <span class="special">|=,</span> 35 <span class="special">-=,</span> <span class="special">&=</span></code> 36 and <code class="computeroutput"><span class="special">^=</span></code> are provided. 37 </p> 38<p> 39 For every <span class="emphasis"><em><span class="bold"><strong>inplace</strong></span></em></span> operator 40</p> 41<pre class="programlisting"><span class="identifier">T</span><span class="special">&</span> <span class="keyword">operator</span> <span class="identifier">o</span><span class="special">=</span> <span class="special">(</span><span class="identifier">T</span><span class="special">&</span> <span class="identifier">object</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">P</span><span class="special">&</span> <span class="identifier">operand</span><span class="special">)</span> 42</pre> 43<p> 44 the <span class="bold"><strong>icl</strong></span> provides corresponding <span class="emphasis"><em><span class="bold"><strong>infix</strong></span></em></span> operators. 45</p> 46<pre class="programlisting"><span class="identifier">T</span> <span class="keyword">operator</span> <span class="identifier">o</span> <span class="special">(</span><span class="identifier">T</span> <span class="identifier">object</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">P</span><span class="special">&</span> <span class="identifier">operand</span><span class="special">){</span> <span class="keyword">return</span> <span class="identifier">object</span> <span class="identifier">o</span><span class="special">=</span> <span class="identifier">operand</span><span class="special">;</span> <span class="special">}</span> 47<span class="identifier">T</span> <span class="keyword">operator</span> <span class="identifier">o</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">P</span><span class="special">&</span> <span class="identifier">operand</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">object</span><span class="special">){</span> <span class="keyword">return</span> <span class="identifier">object</span> <span class="identifier">o</span><span class="special">=</span> <span class="identifier">operand</span><span class="special">;</span> <span class="special">}</span> 48</pre> 49<p> 50 </p> 51<p> 52 From this implementation of the infix <code class="computeroutput"><span class="keyword">operator</span> 53 <span class="identifier">o</span></code> the compiler will hopefully use 54 return value optimization <a href="http://en.wikipedia.org/wiki/Return_value_optimization" target="_top">(RVO)</a> 55 creating no temporary object and performing one copy of the first argument 56 <code class="computeroutput"><span class="identifier">object</span></code>. 57 </p> 58<div class="caution"><table border="0" summary="Caution"> 59<tr> 60<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../doc/src/images/caution.png"></td> 61<th align="left">Caution</th> 62</tr> 63<tr><td align="left" valign="top"><p> 64 Compared to the <span class="emphasis"><em>inplace</em></span> <code class="computeroutput"><span class="keyword">operator</span> 65 <span class="identifier">o</span><span class="special">=</span></code> 66 every use of an <span class="emphasis"><em>infix</em></span> <code class="computeroutput"><span class="keyword">operator</span> 67 <span class="identifier">o</span></code> requires <span class="emphasis"><em><span class="bold"><strong>one extra copy</strong></span></em></span> of the first argument 68 <code class="computeroutput"><span class="identifier">object</span></code> that passes a container. 69 </p></td></tr> 70</table></div> 71<p> 72 Use infix operators only, if 73 </p> 74<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 75<li class="listitem"> 76 efficiency is not crucial, e.g. the containers copied are small. 77 </li> 78<li class="listitem"> 79 a concise and short notation is more important than efficiency in your 80 context. 81 </li> 82<li class="listitem"> 83 you need the result of operator <code class="computeroutput"><span class="identifier">o</span><span class="special">=</span></code> as a copy anyway. 84 </li> 85</ul></div> 86<h6> 87<a name="boost_icl.implementation.inplace_and_infix_operators.h0"></a> 88 <span class="phrase"><a name="boost_icl.implementation.inplace_and_infix_operators.time_complexity_of_infix__code__phrase_role__identifier__operators__phrase___phrase_role__identifier__o__phrase___code_"></a></span><a class="link" href="inplace_and_infix_operators.html#boost_icl.implementation.inplace_and_infix_operators.time_complexity_of_infix__code__phrase_role__identifier__operators__phrase___phrase_role__identifier__o__phrase___code_">Time 89 Complexity of infix <code class="computeroutput"><span class="identifier">operators</span> <span class="identifier">o</span></code></a> 90 </h6> 91<p> 92 The time complexity of all infix operators of the <span class="bold"><strong>icl</strong></span> 93 is biased by the extra copy of the <code class="computeroutput"><span class="identifier">object</span></code> 94 argument. So all infix <code class="computeroutput"><span class="identifier">operators</span> 95 <span class="identifier">o</span></code> are at least <span class="emphasis"><em>linear</em></span> 96 in <code class="computeroutput"><span class="identifier">n</span> <span class="special">=</span> 97 <span class="identifier">object</span><span class="special">.</span><span class="identifier">iterative_size</span><span class="special">()</span></code>. 98 Taking this into account, the complexities of all infix operators can be 99 determined from the corresponding inplace <code class="computeroutput"><span class="identifier">operators</span> 100 <span class="identifier">o</span><span class="special">=</span></code> 101 they depend on. 102 </p> 103</div> 104<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 105<td align="left"></td> 106<td align="right"><div class="copyright-footer">Copyright © 2007-2010 Joachim 107 Faulhaber<br>Copyright © 1999-2006 Cortex Software 108 GmbH<p> 109 Distributed under the Boost Software License, Version 1.0. (See accompanying 110 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 111 </p> 112</div></td> 113</tr></table> 114<hr> 115<div class="spirit-nav"> 116<a accesskey="p" href="complexity.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../implementation.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../function_reference.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 117</div> 118</body> 119</html> 120