<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-26653368</id><updated>2011-12-29T07:32:14.142+05:30</updated><category term='C++'/><category term='C++ books'/><category term='templates'/><category term='return value optimization'/><category term='Boost libraries'/><category term='introduction'/><category term='list of C++ books'/><category term='copy elision'/><category term='smart pointers'/><category term='howto'/><category term='RVO'/><title type='text'>The C++ and Boost Code Shop</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-26653368.post-1047377095242172532</id><published>2010-11-04T18:35:00.007+05:30</published><updated>2010-11-05T22:17:33.124+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='copy elision'/><category scheme='http://www.blogger.com/atom/ns#' term='return value optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='RVO'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Return your objects by value (at least sometimes)</title><content type='html'>&lt;span class="fullpost"&gt;Starting &lt;a href="/2009/02/jargon-time-l-values-r-values-and.html"&gt;where&lt;/a&gt; we left a year and a half back (and I swear I had no time for the blog in this intervening period), we'll look at temporaries again, but in a slightly different light. We saw in that article that it's a great idea to eliminate temporaries of non-POD types (or more correctly, types with non-trivial copy semantics) as far as possible. One big advantage of eliminating temporaries is the elimination of redundant copying from a temporary to a named instance which is how a lot of temporaries inevitably end up. Common ways in which temporaries get created are when objects are returned by value from a function call, and also when a function (say foo) is invoked in-place in the argument list of the invocation of another function (say bar) - and thus the return value of foo() is passed to bar(). Some of these cases are illustrated in the following code:&lt;br /&gt;&lt;pre class="brush: cpp; highlight: [3, 7, 8]"&gt;&lt;br /&gt;Fud foo(int i)  // Fud is a copiable class&lt;br /&gt;{&lt;br /&gt;  return Fud(i); // Temporary created&lt;br /&gt;}&lt;br /&gt;void bar(Fud);&lt;br /&gt;...&lt;br /&gt;bar(foo(1));  // Temporary passed to bar&lt;br /&gt;Fud f = foo(2);&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now suppose we were to rewrite the above code to eliminate temporaries. There are a few different ways, but let's try a fairly simple approach:&lt;br /&gt;&lt;pre class="brush: cpp; highlight: [5]"&gt;&lt;br /&gt;Fud foo(int i)  // Fud is a copiable class&lt;br /&gt;{&lt;br /&gt;  return Fud(i); // Temporary created&lt;br /&gt;}&lt;br /&gt;void bar(const Fud&amp;);&lt;br /&gt;...&lt;br /&gt;bar(foo(1));  // const-reference to temporary, no copying&lt;br /&gt;Fud f = foo(2);&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In the above code, we have eliminated the pass by value of a Fud object to bar(...) but foo(...) still returns a temporary by value. Here is an enhancement:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp; highlight: [1, 3, 5, 7, 8, 9]"&gt;&lt;br /&gt;void foo(int i, Fud *&amp;f)  // Fud is a copiable class&lt;br /&gt;{&lt;br /&gt;  f = new Fud(i);&lt;br /&gt;}&lt;br /&gt;void bar(const Fud&amp;);&lt;br /&gt;...&lt;br /&gt;Fud *pfud = NULL;&lt;br /&gt;foo(2, pfud);&lt;br /&gt;bar(*pfud);  // const-reference to temporary, no copying&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This time there don't seem to be any temporaries and consequently no redundant copying. But the code is no longer simple. At the least, you cannot do something like bar(foo(...)) any more - nesting calls is a natural algebraic operation used to compose functions but the elimination of references takes that ability away from us. Strictly speaking, with Boost shared_ptr, we could eliminate this limitation (how? left as an exercise to the reader, an author's exclusive prerogative). But it still means that we have to incur the cost of dynamic memory allocation. Again, dynamic memory allocation is not necessarily evil - sometimes, it is even more welcome than allocating a large stack based object. But the overall readability of code has suffered as well.&lt;br /&gt;What do we do? Well, it depends.&lt;/span&gt; A good idea is to start by passing temporaries back by value. Now don't cross your eyes - what you just read is exactly what I said and there is a little something that the C++ standard allows (without mandating) and most standard compilers implement as an optimization, which makes this possible. It is called &lt;em&gt;Copy Elision&lt;/em&gt; and in a special form, &lt;em&gt;Return Value Optimization&lt;/em&gt; or RVO for short.&lt;span class="fullpost"&gt;&lt;h4&gt;Copy Elision&lt;/h4&gt;&lt;br /&gt;Copy elision simply refers to elimination of redundant copying, if at all possible. For example, consider the following code:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Fud obj = Fud(1);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;What's happening there? A Fud instance called obj is initialized from a temporary Fud object created through Fud(1). Normally, we would expect that Fud's constructor will be invoked to create a Fud object with an initialization parameter value of 1. Next, the instance called obj will be created and initialized through a call to its copy-constructor which will copy the state of the temporary object to obj. However, it is not hard to see that the only purpose of the temporary object is to help initialize the obj instance. This code could have been written in a much simpler way as:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Fud obj(1);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;There would have been no calls necessary to the copy constructor of obj, nor would there be any temporary instance to destruct. The behaviour of the code would have been exactly the same (unless of course the copy constructor or destructor had side-effects - always a bad idea). Copy elision is the inbuilt optimization in the compiler which causes code like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Fud obj = Fud(1);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;to generate the equivalent of code like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Fud obj(1);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;and thus prevent needless copying. An interesting special case is Return Value Optimization (RVO). It is best illustrated with the following example:&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;#include &amp;lt;vector&gt;&lt;br /&gt;#include &amp;lt;string&gt;&lt;br /&gt;#include &amp;lt;iostream&gt;&lt;br /&gt;&lt;br /&gt;using std::vector;&lt;br /&gt;using std::string;&lt;br /&gt;using std::cout;&lt;br /&gt;using std::endl;&lt;br /&gt;&lt;br /&gt;struct TestClass&lt;br /&gt;{&lt;br /&gt;    TestClass(int i) : i_(i)&lt;br /&gt;    {}&lt;br /&gt;&lt;br /&gt;    TestClass(const TestClass&amp; tc) : i_(tc.i_)&lt;br /&gt;    {&lt;br /&gt;        cout &amp;lt;&amp;lt; "Copied." &amp;lt;&amp;lt; endl;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;private:&lt;br /&gt;    int i_;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;vector&amp;lt;TestClass&gt; make_vec()&lt;br /&gt;{&lt;br /&gt;    vector&amp;lt;TestClass&gt; vec;&lt;br /&gt;    vec.reserve(20);&lt;br /&gt;&lt;br /&gt;    cout &amp;lt;&amp;lt; "Starting populating vector." &amp;lt;&amp;lt; endl;&lt;br /&gt;    for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;        vec.push_back(TestClass(i));&lt;br /&gt;    }&lt;br /&gt;    cout &amp;lt;&amp;lt; "Vector populated." &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;    return vec;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    vector&amp;lt;TestClass&gt; vec = make_vec();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Look at line 40 above. The function make_vec() returns a vector which is copied to the vector vec - what looks like copy initialization. Since the contained type of the vector is TestClass, you'd expect the TestClass instances to be copied, first time when they are enqueued in the vector inside make_vec (line 31) and again when the returned temporary vector from make_vec is used for copy-initialization of vec at line 40. That would be conformant behaviour, but that's often not the behaviour you get. Running on gcc 4.3.2 on OpenSuSE gave me this output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Starting populating vector.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Vector populated.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This clearly shows that there is no copying during copy-initialization of vec - in other words there is no copy-initialization. This is an example of Copy Elision - a special case known as RVO. Internally, the compiler might generate code that arranges for a reference to vec on line 40 to be passed to the function make_vec, and obviate the local vector inside make_vec so that all push_back operations happen on this reference instead. A slight change to the above code can completely disable Copy Elision / RVO, as illustrated below:&lt;br /&gt;&lt;pre class="brush: cpp; first-line: 35; highlight: [40, 41]"&gt;&lt;br /&gt;    return vec;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    vector&amp;lt;TestClass&gt; vec;&lt;br /&gt;    vec = make_vec();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I got this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Starting populating vector.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Vector populated.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;Copied.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Quite clearly, the copy-initialization takes place and has not been optimized away.&lt;br /&gt;&lt;br /&gt;The big advantage of RVO is that the syntax of function calls can be made to match the semantics of the mapping that the function represents. The &lt;em&gt;return value&lt;/em&gt; rather than an &lt;em&gt;out-parameter&lt;/em&gt; is a return value. This also allows for nested function calls, a natural algebraic expression of functional composition.&lt;br /&gt;&lt;br /&gt;Therefore, depending on how the return value of function is going to be used - it might be a good idea to return an object by value.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-1047377095242172532?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/1047377095242172532/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=1047377095242172532' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/1047377095242172532'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/1047377095242172532'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2010/11/copy-your-temporaries-return-by-value.html' title='Return your objects by value (at least sometimes)'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-538097349317931305</id><published>2009-02-02T01:41:00.030+05:30</published><updated>2010-11-04T03:00:50.240+05:30</updated><title type='text'>Jargon Time: L-values, R-values and Temporaries</title><content type='html'>As promised here are a few fairly basic examples of C++ jargon, demystified, here in this article. We start looking at basics of C++ expressions: &lt;i&gt;l-values&lt;/i&gt;, &lt;i&gt;r-values&lt;/i&gt; and &lt;i&gt;temporaries&lt;/i&gt;. The concepts are simple, but important, and would be used in later columns of this (Jargon) series.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;h4&gt;L-values and R-values&lt;/h4&gt;&lt;br /&gt;When we write code, we express action and intent through well-formed expressions that conform to a broad syntax. Some of this action involves moving data around, some of it involves carrying out a more complex operation - and most involve both. For example:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum090201X03" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum090201X03')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum090201X03"&gt;&lt;a href="javascript:hideLineNums('linenum090201X03')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum090201X03" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;double&lt;/span&gt; number = 0.0;&lt;br /&gt;&lt;span class="linenum090201X03" style="display:none;background:silver"&gt;    2&lt;/span&gt; number = 2.0;&lt;br /&gt;&lt;span class="linenum090201X03" style="display:none;background:silver"&gt;    3&lt;/span&gt; &lt;span style="color:blue"&gt;double&lt;/span&gt; square_root = ::sqrt(number);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The above code involves both moving data around, and carrying out some action. In the second line, the literal double &lt;i&gt;2.0&lt;/i&gt; is assigned to the double variable &lt;i&gt;number&lt;/i&gt;. In this context &lt;i&gt;number&lt;/i&gt; is an l-value expression - because it allows modification of the value it holds, when used on the left hand side of an assignment expression.&lt;br /&gt;&lt;br /&gt;The expression &lt;i&gt;2.0&lt;/i&gt; on the other hand can only be used to assign values to expressions such as &lt;i&gt;number&lt;/i&gt; - in other words it can only be used on the right hand side of assignment expressions, never on the left hand side. It can also be used in function return statements. Such expressions are called r-values.&lt;br /&gt;&lt;br /&gt;At this point we have three small observations to make:&lt;br /&gt;1. An r-value can never be used on the left hand side of an assignment operation.&lt;br /&gt;2. An l-value in very many cases can be used on the right hand side of assignment operations also. In this case, it simply degenerates to the value it contains. For example:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum090201X04" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum090201X04')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum090201X04"&gt;&lt;a href="javascript:hideLineNums('linenum090201X04')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum090201X04" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;double&lt;/span&gt; number = 0.0;&lt;br /&gt;&lt;span class="linenum090201X04" style="display:none;background:silver"&gt;    2&lt;/span&gt; number = 2.0;&lt;br /&gt;&lt;span class="linenum090201X04" style="display:none;background:silver"&gt;    3&lt;/span&gt; &lt;span style="color:blue"&gt;double&lt;/span&gt; anotherNumber = number;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In the above code snippet, on the third line, the expression &lt;i&gt;number&lt;/i&gt; is used as an r-value and degenerates to the value contained in the l-value expression &lt;i&gt;number&lt;/i&gt;.&lt;br /&gt;Some l-values cannot be used as r-values. For example, in the above code snippet, the expression &lt;i&gt;double anotherNumber&lt;/i&gt; is an l-value expression, but we cannot write code like:&lt;br /&gt;&lt;br /&gt;double aThirdNumber = (double anotherNumber = 2.0);&lt;br /&gt;&lt;br /&gt;Language rules do not allow this. So you have an example of an l-value expression that cannot degenerate to an r-value expression.&lt;br /&gt;3. l-values can also be used in function return statements. However, whether it is treated as an l-value or simply degenerates to an r-value depends on the return type of the function. If a function returns a non-const reference or pointer to an object, the function call can be considered as an l-value expression. That's essentially because the return value of the function is an l-value. For example:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum090201X01')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum090201X01"&gt;&lt;a href="javascript:hideLineNums('linenum090201X01')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;!-- &lt;span class="linenum090201X01" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;int&lt;/span&gt; size&amp;gt;&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue"&gt;struct&lt;/span&gt; CheckedIntArray {&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;    3&lt;/span&gt;  &lt;span style="color:blue"&gt;int&lt;/span&gt;&amp;amp; &lt;span style="color:blue"&gt;operator&lt;/span&gt;[](&lt;span style="color:blue"&gt;int&lt;/span&gt; index) {&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;    4&lt;/span&gt;   &lt;span style="color:blue"&gt;if&lt;/span&gt; (index &amp;gt;=0 &amp;amp;&amp;amp; index &amp;lt; size) {&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;    5&lt;/span&gt;    &lt;span style="color:blue"&gt;return&lt;/span&gt; array_[index];&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;    6&lt;/span&gt;   }&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;    7&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;    8&lt;/span&gt;   &lt;span style="color:blue"&gt;throw&lt;/span&gt; IndexOutOfBoundsException; &lt;span style="color:teal"&gt;// some exception&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;    9&lt;/span&gt;  }&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;   10&lt;/span&gt; &lt;span style="color:blue"&gt;private&lt;/span&gt;:&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;   11&lt;/span&gt;  &lt;span style="color:blue"&gt;int&lt;/span&gt; array_[size];&lt;br /&gt;&lt;span class="linenum090201X01" style="display:none;background:silver"&gt;   12&lt;/span&gt; };&lt;br /&gt; --&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;template&amp;lt;int size&gt;&lt;br /&gt;struct CheckedIntArray {&lt;br /&gt;  int&amp; operator[](int index) {&lt;br /&gt;   if (index &gt;=0 &amp;&amp; index &amp;lt; size) {&lt;br /&gt;    return array_[index];&lt;br /&gt;   }&lt;br /&gt;   throw IndexOutOfBoundsException; // some exception&lt;br /&gt;  }&lt;br /&gt; private:&lt;br /&gt;  int array_[size];&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In the above CheckedIntArray class, operator[](int) can actually be used as an l-value expression because it returns a reference to an element in the underlying array. This enables use to write code like this:&lt;br /&gt;&lt;span class="linenum090201X02" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum090201X02')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum090201X02"&gt;&lt;a href="javascript:hideLineNums('linenum090201X02')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum090201X02" style="display:none;background:silver"&gt;    1&lt;/span&gt; CheckedIntArray&amp;lt;16&amp;gt; my_array;&lt;br /&gt;&lt;span class="linenum090201X02" style="display:none;background:silver"&gt;    2&lt;/span&gt; my_array[0] = 15;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In the above, the expression &lt;i&gt;my_array[0] = 15;&lt;/i&gt; is equivalent to &lt;i&gt;my_array.operator[](0) = 15;&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;Many complex expressions are r-values. As well as a few simple ones:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum090201X05" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum090201X05')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum090201X05"&gt;&lt;a href="javascript:hideLineNums('linenum090201X05')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum090201X05" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; i = 0;&lt;br /&gt;&lt;span class="linenum090201X05" style="display:none;background:silver"&gt;    2&lt;/span&gt; ++i; &lt;span style="color:teal"&gt;// r-value expression&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Arrays are r-values although individual elements in an array are not. Of course this does not apply to a pointer being used with an array syntax. Thus:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum090202X09" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum090202X09')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum090202X09"&gt;&lt;a href="javascript:hideLineNums('linenum090202X09')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum090202X09" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; arr[32] = {0};&lt;br /&gt;&lt;span class="linenum090202X09" style="display:none;background:silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; arr2[32] = {1};&lt;br /&gt;&lt;span class="linenum090202X09" style="display:none;background:silver"&gt;    3&lt;/span&gt; arr[0] = 5;  &lt;span style="color:teal"&gt;// arr[0] is an l-value&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum090202X09" style="display:none;background:silver"&gt;    4&lt;/span&gt; &lt;span style="color:teal"&gt;// the following is illegal&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum090202X09" style="display:none;background:silver"&gt;    5&lt;/span&gt; &lt;span style="color:teal"&gt;// arr = arr2; // arr is an r-value&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum090202X09" style="display:none;background:silver"&gt;    6&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum090202X09" style="display:none;background:silver"&gt;    7&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Finally, let it be said that all expressions in C++ are either l-value or r-value expressions.&lt;br /&gt;&lt;h4&gt;Temporaries&lt;/h4&gt;&lt;br /&gt;Related to the concept of r-values is the concept of temporaries. In fact temporaries are r-values (without all r-values being temporaries). Consider the following example:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum090201X06" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum090201X06')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum090201X06"&gt;&lt;a href="javascript:hideLineNums('linenum090201X06')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum090201X06" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; m = 4;&lt;br /&gt;&lt;span class="linenum090201X06" style="display:none;background:silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; n = 5 + 8/m;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here the expression &lt;i&gt;5 + 8/m&lt;/i&gt; is a temporary. This is a relatively simple temporary - possibly one that would only exist in the registers of the CPU. However, it is possible, and quite common, to have temporaries on the stack. The important thing to understand is that temporaries are unnamed values, which are created in the context of an expression and whose life time is limited to the period of evaluation of that expression. Consider the following expression:&lt;br /&gt;&lt;br /&gt;string str = string("Hola amigos!");&lt;br /&gt;&lt;br /&gt;The right hand side expression creates a temporary string object, and it is then copied to a local variable called str. Once the control of the executing program reaches past the semi-colon terminating this line of code, the temporary object is gone. Only str, containing a copy of it, exists.&lt;br /&gt;&lt;br /&gt;There is one exception to this rule and it deals with references. In the last expression, if instead of a string variable on the left, we had a string reference, things would be a little different:&lt;br /&gt;&lt;br /&gt;const string&amp; str = string("Hola amigos!");&lt;br /&gt;&lt;br /&gt;First of all, if you see we've had to add a const to the reference. We could not have had a non-const reference to a temporary. This is always the case, as you can see below:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum090201X07" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum090201X07')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum090201X07"&gt;&lt;a href="javascript:hideLineNums('linenum090201X07')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum090201X07" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;const&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt;&amp;amp; r = 5;&lt;br /&gt;&lt;span class="linenum090201X07" style="display:none;background:silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue"&gt;const&lt;/span&gt; &lt;span style="color:blue"&gt;double&lt;/span&gt;&amp;amp; s = 2.0;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Since all temporaries are r-values, it is clear a non-const reference cannot refer to them. But, the exception that I referred to is in the life time of the temporary when a (const) reference refers to it. In this case, the temporary persists till the reference is in scope, and not just till the end of the statement that created the reference.&lt;br /&gt;&lt;br /&gt;References are often created for function return values, although most optimizing compilers would eliminate the creation of these temporaries if the return value of the function was not assigned to any specific object. In general, reducing the number of temporaries that is created by a program is a good strategy for optimization, and to some extent, the compiler already does it.&lt;br /&gt;&lt;br /&gt;Since all temporaries are r-values, it is clear a non-const reference cannot refer to them. But, the exception that I referred to is in the life time of the temporary when a (const) reference refers to it. In this case, the temporary persists till the reference is in scope, and not just till the end of the statement that created the reference.&lt;br /&gt;&lt;br /&gt;References are often created for function return values, although most optimizing compilers would eliminate the creation of these temporaries if the return value of the function was not assigned to any specific object. In general, reducing the number of temporaries that is created by a program is a good strategy for optimization, and to some extent, the compiler already does it.&lt;br /&gt;&lt;br /&gt;As a final example of how temporaries are generated, and where we can run into trouble with them if we are not careful, I present a piece of code I have seen written in several places (including products I have worked on).&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum090201X08')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum090201X08"&gt;&lt;a href="javascript:hideLineNums('linenum090201X08')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;using&lt;/span&gt; std::string;&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue"&gt;using&lt;/span&gt; std::stringstream;&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;    3&lt;/span&gt; &lt;span style="color:blue"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;    4&lt;/span&gt; &lt;span style="color:blue"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;    5&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;    6&lt;/span&gt; ...&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;    7&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;    8&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; x = 0;&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;    9&lt;/span&gt; &lt;span style="color:blue"&gt;double&lt;/span&gt; f = 1.6;&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;   10&lt;/span&gt; stringstream sout;&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;   11&lt;/span&gt; sout &amp;lt;&amp;lt; &lt;span style="color:red"&gt;"Some data values streamed: "&lt;/span&gt; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &lt;span style="color:red"&gt;"|"&lt;/span&gt; &amp;lt;&amp;lt; f;&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;   12&lt;/span&gt; &lt;span style="color:blue"&gt;const&lt;/span&gt; &lt;span style="color:blue"&gt;char&lt;/span&gt; *str = sout.str().c_str();&lt;br /&gt;&lt;span class="linenum090201X08" style="display:none;background:silver"&gt;   13&lt;/span&gt; cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; endl;  &lt;span style="color:teal"&gt;// this will likely print garbage&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Can you spot the trouble with the above code. The trouble is that the member function &lt;i&gt;std::string str() const&lt;/i&gt; of the std::stringstream class returns a temporary string. But in the expression &lt;i&gt;sout.str().c_str()&lt;/i&gt;, we get a reference to the &lt;i&gt;const char*&lt;/i&gt; pointer member of the returned temporary string and we copy it to the variable called str. As soon as this statement is executed, the temporary that was created as a result of the call to sout.str() is destroyed. But we still have a dangling pointer referring to its internal char * string, which is invalid for all good money. Needless to say, the last line above can even crash the program itself.&lt;br /&gt;&lt;br /&gt;In the next edition of the Jargon column, we'll look at &lt;b&gt;Namespace lookups&lt;/b&gt; and &lt;b&gt;the Interface Principle&lt;/b&gt;. Keep watching, for more jargons demystified.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-538097349317931305?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/538097349317931305/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=538097349317931305' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/538097349317931305'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/538097349317931305'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2009/02/jargon-time-l-values-r-values-and.html' title='Jargon Time: L-values, R-values and Temporaries'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-8878466866950221590</id><published>2009-02-01T13:02:00.003+05:30</published><updated>2009-02-01T13:15:00.077+05:30</updated><title type='text'>Parlez vous le C++?</title><content type='html'>&lt;span class="fullpost"&gt;Recently I was thinking back on my days of trying to learn C++ by reading the Usenet groups &lt;a href="http://groups.google.com/group/comp.lang.c++.moderated"&gt;comp.lang.c++.moderated&lt;/a&gt; and &lt;a href="http://groups.google.com/group/comp.std.c++"&gt;comp.std.c++&lt;/a&gt;. If someone asks me the best source to learn C++ from, I would always refer to these two places (and perhaps the Boost mailing list even though it is a little more Boost-focussed). Note that I said the best source to "learn" C++ - not merely "read" it (for which &lt;a href="http://shoddykid.blogspot.com/2008/07/book-mania.html"&gt;here&lt;/a&gt; are the books).&lt;br /&gt;&lt;br /&gt;I'll explain what I mean by that (and as usual, I'll get a bit philosophical before cutting to the chase). I read C++ on my own, almost every written word of it that I read, alone - not a soul around even to discuss, not a teacher around to cast an impression. And yet no learning is complete unless an impression of that knowledge has been cast on us - this is true for every field of learning - even learning to memorize the English alphabet. The reason must be that this impression is cast on us through multiple senses - sight and sound, if not more. On the other hand, reading a tome is one dimensional - sight. It is here that the Usenet discussion boards and other mailing lists step in. While you still only read, you read direct discussions, arguments, brain-storms, doubts, misgivings, biases - it is a lot more real than reading a chapter on C++ polymorphism. It is what eggs you on to introspect - identify with the views expressed, or refute them; in other words, your C++ perspective is built here and that is what I meant when I said "learning".&lt;br /&gt;&lt;br /&gt;Usenet in particular provided this motivation - I have seen posts by Bjarne Stroustrup, Scott Meyers, Andrei Alexandrescu, Herb Sutter, Jim Coplien, Steve Dewhurst, Andrew König, you name one, I'd have read his posts. The debates would go on for days together, even weeks, in lists that grew deep and wide with time. I was a mere reader - my occasional two-pence in the middle of debates would be politely answered but I had not the knowledge or understanding of the language to make a serious impact. &lt;/span&gt;Half the time, I groped through the standard or Stroustrup's book trying to figure out what is a "temporary" or where are "incomplete types" allowed, what is the meaning of "SFINAE" or what is the "Liskov Substitution Principle". You could be excused for wondering "Is this a programming language we are reading or a Theory of Banach algebras?". I certainly did - but it was part of C++'s charm (oh! geek) - for a wannabe-mathematician-turned-nobody, this was a nice feel-good aberration in a language I wanted to "speak". But coming back to the point - this meant I had to have a basic vocabulary ready, and a familiarity with a C++ alphabet soup before I could start communicating effectively in real C++ terms. C++ is tough and exacting, it requires discipline and knowledge - and it was not defined by a Sun or a Microsoft. I plan to put up a set of articles that would help build the awareness and concepts around the C++ jargons. Happy reading!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-8878466866950221590?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/8878466866950221590/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=8878466866950221590' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/8878466866950221590'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/8878466866950221590'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2009/02/parlez-vous-le-c.html' title='Parlez vous le C++?'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-1846229172641545983</id><published>2008-08-13T02:47:00.018+05:30</published><updated>2010-11-04T13:21:45.546+05:30</updated><title type='text'>Aliasing pointers: using "weakness" to strengthen your code</title><content type='html'>In one of my earlier articles introducing programming with Boost libraries, I had demonstrated some simple techniques using boost::shared_ptr to share dynamically allocated object instances across scopes, and manage the life cycle of such objects effectively. This was an important step in the direction of eliminating memory leaks. But this was not a complete answer to all forms of memory leaks. This article helps you distinguish between "owning" and "aliasing" semantics for pointers and introduces another class from Boost's smart pointer stable - the boost::weak_ptr. Although not a smart pointer itself, the weak_ptr adds important capability that is vital in handling memory issues and thread safety of objects.&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;Imagine that you are building a linked list, and you decide to store the "head" node as a shared_ptr&amp;lt;Node&amp;gt; and encapsulate inside this node, the life cycle management of all the nodes in the entire list. That way, when the "head" node goes out of scope, all the other nodes are automatically deleted. How can you do it? Well, the head pointer stores a shared_ptr&amp;lt;Node&amp;gt; pointing to the first node, the first node stores a shared_ptr&amp;lt;Node&amp;gt; pointing to the second node, and so on.&lt;br /&gt;&lt;br /&gt;Here is the code, and it works.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;#include &amp;lt;iostream&gt;&lt;br /&gt;#include &amp;lt;boost/shared_ptr.hpp&gt;&lt;br /&gt;&lt;br /&gt;using std::cout;&lt;br /&gt;using std::endl;&lt;br /&gt;using boost::shared_ptr;&lt;br /&gt;&lt;br /&gt;struct Node {&lt;br /&gt;    int n_;&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next;&lt;br /&gt;&lt;br /&gt;    Node(int n = 0) : n_(n) {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    ~Node() {&lt;br /&gt;        cout &amp;lt;&amp;lt; "~Node() invoked for " &amp;lt;&amp;lt; n_ &amp;lt;&amp;lt; endl;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;shared_ptr&amp;lt;Node&gt; getList() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; head( new Node(0) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next1( new Node(1) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next2( new Node(2) );&lt;br /&gt;&lt;br /&gt;    head-&gt;next = next1;&lt;br /&gt;    next1-&gt;next = next2;&lt;br /&gt;&lt;br /&gt;    return head;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; head = getList();&lt;br /&gt;    if ( head )&lt;br /&gt;        cout &amp;lt;&amp;lt; "Got head" &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This works remarkably well, and by now your penchant for using shared_ptr and RAII is seeing you write elegant code and wish away memory leaks. And then you are asked to enhance your linked list to a doubly linked list. With a deft, touch you make the necessary changes - and you are up and running again ...&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp; highlight: [11, 28, 29]"&gt;&lt;br /&gt;#include &amp;lt;iostream&gt;&lt;br /&gt;#include &amp;lt;boost/shared_ptr.hpp&gt;&lt;br /&gt;&lt;br /&gt;using std::cout;&lt;br /&gt;using std::endl;&lt;br /&gt;using boost::shared_ptr;&lt;br /&gt;&lt;br /&gt;struct Node {&lt;br /&gt;    int n_;&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next;&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; prev;&lt;br /&gt;&lt;br /&gt;    Node(int n = 0) : n_(n) {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    ~Node() {&lt;br /&gt;        cout &amp;lt;&amp;lt; "~Node() invoked for " &amp;lt;&amp;lt; n_ &amp;lt;&amp;lt; endl;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;shared_ptr&amp;lt;Node&gt; getList() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; head( new Node(0) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next1( new Node(1) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next2( new Node(2) );&lt;br /&gt;&lt;br /&gt;    head-&gt;next = next1;&lt;br /&gt;    next1-&gt;next = next2;&lt;br /&gt;    next1-&gt;prev = head;&lt;br /&gt;    next2-&gt;prev = next1;&lt;br /&gt;&lt;br /&gt;    return head;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; head = getList();&lt;br /&gt;    if ( head )&lt;br /&gt;        cout &amp;lt;&amp;lt; "Got head" &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;... except that you have a memory leak. Believe it or not, what I promised a couple of articles ago was a fib - you can leak memory without even so much as nibbling on a pointer, and in spite of having wrapped all your heap memory in shared_ptr. Run it and you'll not see any of the messages from the destructor of Node. Come to think of it and you'll know the reason.&lt;br /&gt;&lt;br /&gt;In the function &lt;i&gt;getList&lt;/i&gt;, initially, the nodes &lt;i&gt;head&lt;/i&gt;, &lt;i&gt;next1&lt;/i&gt; and &lt;i&gt;next2&lt;/i&gt; each have a reference count  of 1 (at line 22, 23 and 24 respectively). Next at line 26, &lt;i&gt;next1&lt;/i&gt; has its ref-count bumped up to 2. In the following line 27, &lt;i&gt;next2&lt;/i&gt; has its ref-count bumped up to 2. So far so good - the real fun starts now. In the next line 28, &lt;i&gt;head&lt;/i&gt; has its ref-count bumped to 2, and finally &lt;i&gt;next1&lt;/i&gt; has its ref-count bumped to 3. When this function returns, each of the Smart pointers have their reference counts decreased by 1. But &lt;i&gt;head&lt;/i&gt; is copied as a return value - so the decrement nullifies, and when we reach main function at line 35, its reference count remains 2. At this point, the reference count of next1 and next2 are 2 and 1 respectively - but these objects are no longer in scope - completely encapsulated by &lt;i&gt;head&lt;/i&gt;. At the end of the main function's scope, the ref-count of &lt;i&gt;head&lt;/i&gt; will drop back a notch to 1 - it will not get to 0 - necessary for its internal pointer to get "destructed" and trigger the destruction of its subsequent nodes.&lt;br /&gt;&lt;br /&gt;What caused the problem? We only introduced three lines of code - lines 11, 28 and 29 - so the problem would have to be here in these three lines.&lt;br /&gt;&lt;br /&gt;Why did all hell break lose? Because, we used the shared_ptr indiscriminately. The shared_ptr implements a "shared ownership" idiom. In this case, &lt;i&gt;head&lt;/i&gt; "owns" the &lt;i&gt;next1&lt;/i&gt; node - meaning that before &lt;i&gt;head&lt;/i&gt; is completely "destructed", it would have ensured complete "destruction" of &lt;i&gt;next1&lt;/i&gt;. But &lt;i&gt;next1&lt;/i&gt; does not own &lt;i&gt;head&lt;/i&gt; - it only has an &lt;i&gt;alias&lt;/i&gt; to &lt;i&gt;head&lt;/i&gt;, in the form of the member &lt;i&gt;prev&lt;/i&gt;. But by making it into a shared_ptr&amp;lt;Node&amp;gt;, we have unwittingly made &lt;i&gt;next1&lt;/i&gt; the owner of head. So we hit the chicken-and-egg problem. To reiterate what we just said about ownership semantics - before &lt;i&gt;head&lt;/i&gt; is completely "destructed", it should ensure complete "destruction" of &lt;i&gt;next1&lt;/i&gt;, and before &lt;i&gt;next1&lt;/i&gt; is completely "destructed", it should ensure complete "destruction" of &lt;i&gt;head&lt;/i&gt;. Of course that's an impossible scenario. So we need a different construct for handling the semantics of an aliasing pointer. Here is the code example again with some changes that work reasonably well.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp; highlight: [11, 28, 29]"&gt;&lt;br /&gt;#include &amp;lt;iostream&gt;&lt;br /&gt;#include &amp;lt;boost/shared_ptr.hpp&gt;&lt;br /&gt;&lt;br /&gt;using std::cout;&lt;br /&gt;using std::endl;&lt;br /&gt;using boost::shared_ptr;&lt;br /&gt;&lt;br /&gt;struct Node {&lt;br /&gt;    int n_;&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next;&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; *prev;&lt;br /&gt;&lt;br /&gt;    Node(int n = 0) : n_(n) {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    ~Node() {&lt;br /&gt;        cout &amp;lt;&amp;lt; "~Node() invoked for " &amp;lt;&amp;lt; n_ &amp;lt;&amp;lt; endl;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;shared_ptr&amp;lt;Node&gt; getList() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; head( new Node(0) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next1( new Node(1) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next2( new Node(2) );&lt;br /&gt;&lt;br /&gt;    head-&gt;next = next1;&lt;br /&gt;    next1-&gt;next = next2;&lt;br /&gt;    next1-&gt;prev = &amp;head;&lt;br /&gt;    next2-&gt;prev = &amp;next1;&lt;br /&gt;&lt;br /&gt;    return head;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; head = getList();&lt;br /&gt;    if ( head )&lt;br /&gt;        cout &amp;lt;&amp;lt; "Got head" &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;At this point of time, you decide to remove the first node of the list &lt;i&gt;head&lt;/i&gt;. As part of the process, you must do something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp; highlight: [6]"&gt;&lt;br /&gt;int main() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; new_head;&lt;br /&gt;    {&lt;br /&gt;        shared_ptr&amp;lt;Node&gt; head = getList();&lt;br /&gt;        new_head = head-&gt;next;&lt;br /&gt;        new_head-&gt;prev = 0;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The statement at line 6, is crucial because if prev is not set to 0 at this point, an accidental dereferencing of new_head-&gt;prev at a later point in the code will bomb. At the end of the scope, at line 7, the &lt;i&gt;head&lt;/i&gt; goes out of scope and is "destructed". What if this automatically set the &lt;i&gt;prev&lt;/i&gt; member pointing to it to be NULL. Then we could afford to forget to write new_head-&gt;prev = 0, without facing any dire consquences.&lt;br /&gt;&lt;br /&gt;The boost::weak_ptr is tailor-made for the purpose shown here - it models a "non-owning alias" which is automatically set to a null state when the object it points to is destructed. For this, weak_ptr always works in conjunction with a shared_ptr, and always points to an object via the shared_ptr that owns it. The following example should make all this clear:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp; highlight: [7, 12]"&gt;&lt;br /&gt;#include &amp;lt;boost/shared_ptr.hpp&gt;&lt;br /&gt;#include &amp;lt;boost/weak_ptr.hpp&gt;&lt;br /&gt;&lt;br /&gt;using std::cout;&lt;br /&gt;using std::endl;&lt;br /&gt;using boost::shared_ptr;&lt;br /&gt;using boost::weak_ptr;&lt;br /&gt;&lt;br /&gt;struct Node {&lt;br /&gt;    int n_;&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next;&lt;br /&gt;    weak_ptr&amp;lt;Node&gt; prev;&lt;br /&gt;&lt;br /&gt;    Node(int n = 0) : n_(n) {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    ~Node() {&lt;br /&gt;        cout &amp;lt;&amp;lt; "~Node() invoked for " &amp;lt;&amp;lt; n_ &amp;lt;&amp;lt; endl;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;shared_ptr&amp;lt;Node&gt; getList() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; head( new Node(0) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next1( new Node(1) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next2( new Node(2) );&lt;br /&gt;&lt;br /&gt;    head-&gt;next = next1;&lt;br /&gt;    next1-&gt;next = next2;&lt;br /&gt;    next1-&gt;prev = head;&lt;br /&gt;    next2-&gt;prev = next1;&lt;br /&gt;&lt;br /&gt;    return head;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; head = getList();&lt;br /&gt;    if ( head )&lt;br /&gt;        cout &amp;lt;&amp;lt; "Got head" &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The weak_ptr maintains a reference to a shared_ptr owning the object. But it does not bump the reference count of a shared_ptr - therefore any number of weak_ptr's can point to a shared_ptr, without affecting its life cycle in anyway. If the shared_ptr goes out of scope  and the underlying object is destructed - the weak_ptr's are automatically "updated" about this change through the reference to the shared_ptr that it maintains.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;In an informal way, weak_ptr's are to shared_ptr's, what soft links are to hard links on a Unix file system.&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;You can check the state of the underlying object through the weak_ptr, using the weak_ptr::expired() member function. You can also construct an owning reference - a shared_ptr which contributes to the reference count of the original shared_ptr - through the weak_ptr::lock() member function. This can indirectly be used to check the same condition as weak_ptr::expired() does. To understand that - look at the final example here:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp; highlight: [38, 45, 46, 47, 48, 49]"&gt;&lt;br /&gt;#include &amp;lt;iostream&gt;&lt;br /&gt;#include &amp;lt;boost/shared_ptr.hpp&gt;&lt;br /&gt;#include &amp;lt;boost/weak_ptr.hpp&gt;&lt;br /&gt;&lt;br /&gt;using std::cout;&lt;br /&gt;using std::endl;&lt;br /&gt;using boost::shared_ptr;&lt;br /&gt;using boost::weak_ptr;&lt;br /&gt;&lt;br /&gt;struct Node {&lt;br /&gt;    int n_;&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next;&lt;br /&gt;    weak_ptr&amp;lt;Node&gt; prev;&lt;br /&gt;&lt;br /&gt;    Node(int n = 0) : n_(n) {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    ~Node() {&lt;br /&gt;        cout &amp;lt;&amp;lt; "~Node() invoked for " &amp;lt;&amp;lt; n_ &amp;lt;&amp;lt; endl;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;shared_ptr&amp;lt;Node&gt; getList() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; head( new Node(0) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next1( new Node(1) );&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; next2( new Node(2) );&lt;br /&gt;&lt;br /&gt;    head-&gt;next = next1;&lt;br /&gt;    next1-&gt;next = next2;&lt;br /&gt;    next1-&gt;prev = head;&lt;br /&gt;    next2-&gt;prev = next1;&lt;br /&gt;&lt;br /&gt;    return head;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; new_head;&lt;br /&gt;    weak_ptr&amp;lt;Node&gt; alias;&lt;br /&gt;    {&lt;br /&gt;        shared_ptr&amp;lt;Node&gt; head = getList();&lt;br /&gt;        new_head = head-&gt;next;&lt;br /&gt;        alias = head;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    assert(alias.expired());&lt;br /&gt;&lt;br /&gt;    alias = new_head;&lt;br /&gt;    shared_ptr&amp;lt;Node&gt; new_head2 = alias.lock();&lt;br /&gt;    assert(new_head2.use_count() == 2); // because new_head and new_head2 share ownership&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The lock method is particularly crucial when sharing an object across threads. Some threads might only have an aliasing reference to the shared object in the form of a weak_ptr. This can be used to great effect. At any time when such a thread needs to share ownership of this object - it can call lock on the weak_ptr. It will get a non-null shared_ptr only if the object is still valid, and otherwise it will get a null shared_ptr.&lt;br /&gt;&lt;br /&gt;Notice that the weak_ptr provides no ways of dereferencing the underlying pointer through it. So there are no overloaded -&gt; or * operators for example. If you need to use these operators - use the lock method to try and get a shared_ptr and then apply these methods on the shared_ptr. In fact - a weak_ptr is not a smart pointer - I call it a "smarter" pointer.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-1846229172641545983?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/1846229172641545983/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=1846229172641545983' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/1846229172641545983'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/1846229172641545983'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2008/08/aliasing-pointers-using-weakness-to.html' title='Aliasing pointers: using &quot;weakness&quot; to strengthen your code'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-3668602590297991459</id><published>2008-08-03T00:02:00.008+05:30</published><updated>2008-08-03T04:14:02.769+05:30</updated><title type='text'>Expression Templates Demystified: Part 2</title><content type='html'>In this article, we would see how the use of templates can simplify and generalize the Expression Functors code, and improve performance by eliminating virtual functions.&lt;br /&gt;&lt;br /&gt;In the example from the &lt;a href="/2008/07/expression-templates-demystified.html"&gt;previous segment&lt;/a&gt; of this article, the Expression abstract base class represented the family of all types that could be used to represent different kinds of expressions. Two specific classes, Variable and Constant, represented the leafs of an expression tree - numeric literals and single variable names. These could be combined to form other expressions - generally represented by the ComplexExpression class. All these classes implemented the interface defined by the Expression abstract base class.&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;We now plan to switch from dynamic polymorphism to template driven static polymorphism. For example, given classes with a common interface but without a common base class, like the ones below:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum53" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum53')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum53"&gt;&lt;a href="javascript:hideLineNums('linenum53')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;struct&lt;/span&gt; Foo {&lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue"&gt;void&lt;/span&gt; bar() {&lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;    3&lt;/span&gt;         &lt;span style="color:teal"&gt;// implementation&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;    4&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;    5&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;    6&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue"&gt;struct&lt;/span&gt; Foo2 {&lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;    8&lt;/span&gt;     &lt;span style="color:blue"&gt;void&lt;/span&gt; bar() {&lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;    9&lt;/span&gt;         &lt;span style="color:teal"&gt;// implementation&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;   10&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum53" style="display:none;background:silver"&gt;   11&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;we can wrap them using a class template that has a similar interface as these:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum54" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum54')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum54"&gt;&lt;a href="javascript:hideLineNums('linenum54')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum54" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;class&lt;/span&gt; F&amp;gt;&lt;br /&gt;&lt;span class="linenum54" style="display:none;background:silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue"&gt;struct&lt;/span&gt; AllFoo {&lt;br /&gt;&lt;span class="linenum54" style="display:none;background:silver"&gt;    3&lt;/span&gt;     AllFoo(F&amp;amp; f) : f_(f) {}&lt;br /&gt;&lt;span class="linenum54" style="display:none;background:silver"&gt;    4&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum54" style="display:none;background:silver"&gt;    5&lt;/span&gt;     &lt;span style="color:blue"&gt;void&lt;/span&gt; bar() {&lt;br /&gt;&lt;span class="linenum54" style="display:none;background:silver"&gt;    6&lt;/span&gt;         f_.bar();&lt;br /&gt;&lt;span class="linenum54" style="display:none;background:silver"&gt;    7&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum54" style="display:none;background:silver"&gt;    8&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This enforces that AllFoo can only be instantiated with such classes which have the bar() method in their public interface. Thus we write the following alternate definitions:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum55" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum55')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum55"&gt;&lt;a href="javascript:hideLineNums('linenum55')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;class&lt;/span&gt; E&amp;gt;&lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue"&gt;struct&lt;/span&gt; Expr {&lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;    3&lt;/span&gt;     Expr(E&amp;amp; e) : e_(e) {&lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;    4&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;    5&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;    6&lt;/span&gt;     &lt;span style="color:blue"&gt;double&lt;/span&gt; &lt;span style="color:blue"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue"&gt;double&lt;/span&gt; d) {&lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;    7&lt;/span&gt;         &lt;span style="color:blue"&gt;return&lt;/span&gt; e_(d);&lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;    8&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;    9&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;   10&lt;/span&gt;     E e_;&lt;br /&gt;&lt;span class="linenum55" style="display:none;background:silver"&gt;   11&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here Expr is a class that can encapsulate all expression objects, like the ones of type Constant or Variable below.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum56" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum56')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum56"&gt;&lt;a href="javascript:hideLineNums('linenum56')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;struct&lt;/span&gt; Constant {&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;    2&lt;/span&gt;     Constant(&lt;span style="color:blue"&gt;double&lt;/span&gt; d) : d_(d) { }&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;    3&lt;/span&gt;     Constant(&lt;span style="color:blue"&gt;int&lt;/span&gt; d) : d_(d) { }&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;    4&lt;/span&gt;     &lt;span style="color:blue"&gt;double&lt;/span&gt; &lt;span style="color:blue"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue"&gt;double&lt;/span&gt;) {&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;    5&lt;/span&gt;         &lt;span style="color:blue"&gt;return&lt;/span&gt; d_;&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;    6&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;    7&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;    8&lt;/span&gt;     &lt;span style="color:blue"&gt;double&lt;/span&gt; d_;&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;    9&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;   10&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;   11&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;   12&lt;/span&gt; &lt;span style="color:blue"&gt;struct&lt;/span&gt; Variable {&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;   13&lt;/span&gt;     &lt;span style="color:blue"&gt;double&lt;/span&gt; &lt;span style="color:blue"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue"&gt;double&lt;/span&gt; d) {&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;   14&lt;/span&gt;         &lt;span style="color:blue"&gt;return&lt;/span&gt; d;&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;   15&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum56" style="display:none;background:silver"&gt;   16&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Finally, a class to represent non-terminal, complex expressions:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum57" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum57')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum57"&gt;&lt;a href="javascript:hideLineNums('linenum57')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;class&lt;/span&gt; E1, &lt;span style="color:blue"&gt;class&lt;/span&gt; E2, &lt;span style="color:blue"&gt;class&lt;/span&gt; Op&amp;gt;&lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue"&gt;struct&lt;/span&gt; ComplexExpr {&lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;    3&lt;/span&gt;     ComplexExpr(Expr&amp;lt;E1&amp;gt; l, Expr&amp;lt;E2&amp;gt; r) : l_(l), r_(r) {&lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;    4&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;    5&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;    6&lt;/span&gt;     &lt;span style="color:blue"&gt;double&lt;/span&gt; &lt;span style="color:blue"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue"&gt;double&lt;/span&gt; d) {&lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;    7&lt;/span&gt;         &lt;span style="color:blue"&gt;return&lt;/span&gt; Op::apply(l_(d), r_(d));&lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;    8&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;    9&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;   10&lt;/span&gt;     Expr&amp;lt;E1&amp;gt; l_;&lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;   11&lt;/span&gt;     Expr&amp;lt;E2&amp;gt; r_;&lt;br /&gt;&lt;span class="linenum57" style="display:none;background:silver"&gt;   12&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We have just carried over the definition of the ComplexExpression class from the previous article and made it into a template class. The operator classes like Add, Subtract, Multiply and Divide should continue to work unchanged. Guess what, we just have to define the operator overloads (for +, -, * and /) and we will be done with defining our framework of algebraic expression.&lt;br /&gt;&lt;br /&gt;Now, we would want to write expressions such as:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;Variable x;&lt;br /&gt;cout &lt;&lt; (x+3)(2); // prints 5&lt;br /&gt;cout &lt;&lt; ((x*x+3)*(x+3))(2); // prints 35&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In the above, x is a Variable, 3 should generate a Constant, x + 3 should result in a ComplexExpr&amp;lt;Variable, Constant&amp;gt;. Further, x*x is a ComplexExpr&amp;lt;Variable, Variable&amp;gt; and (x*x+3)*(x+3) is a ComplexExpr&amp;lt;ComplexExpr&amp;lt; ComplexExpr&amp;lt;Variable, Variable&amp;gt;, Constant&amp;gt;, ComplexExpr&amp;lt;Variable, Constant&amp;gt; &amp;gt;. One can trace the template parameter types as sub-trees of the class template ComplexExpression.&lt;br /&gt;&lt;br /&gt;Clearly, to write an expression like x+3, we need an overload of operator+ between a Variable and an integer. To write an expression like (x*x+3)*(x+3) - we need an operator* between two ComplexExpressions. To write something like, x+Constant(3), we need an overload of operator+ between Variable and Constant. Now, just as we defined the basic operators on Expression in the previous article, we could do the same on the Expr class template here, instead of overloading each operator for different combinations of types. For example:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum58" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum58')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum58"&gt;&lt;a href="javascript:hideLineNums('linenum58')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum58" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;class&lt;/span&gt; E1, &lt;span style="color:blue"&gt;class&lt;/span&gt; E2&amp;gt;&lt;br /&gt;&lt;span class="linenum58" style="display:none;background:silver"&gt;    2&lt;/span&gt; Expr&amp;lt;ComplexExpr&amp;lt;Expr&amp;lt;E1&amp;gt;, Expr&amp;lt;E2&amp;gt;, Add&amp;gt; &amp;gt; &lt;span style="color:blue"&gt;operator&lt;/span&gt;+ (E1 e1, E2 e2) {&lt;br /&gt;&lt;span class="linenum58" style="display:none;background:silver"&gt;    3&lt;/span&gt;     &lt;span style="color:blue"&gt;typedef&lt;/span&gt; ComplexExpr&amp;lt;Expr&amp;lt;E1&amp;gt;, Expr&amp;lt;E2&amp;gt;, Add&amp;gt; ExprType;&lt;br /&gt;&lt;span class="linenum58" style="display:none;background:silver"&gt;    4&lt;/span&gt;     &lt;span style="color:blue"&gt;return&lt;/span&gt; Expr&amp;lt;ExprType&amp;gt;( ExprType(Expr&amp;lt;E1&amp;gt;(e1), Expr&amp;lt;E2&amp;gt;(e2)) );&lt;br /&gt;&lt;span class="linenum58" style="display:none;background:silver"&gt;    5&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The other operators are not very difficult to write, following the above code. However, turns out that while these do take care of operators between complex expressions as well as Variables and Constants, they cannot handle real number or integer literals. To handle real number literals, we define the following overload pairs for each operator.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum59" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum59')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum59"&gt;&lt;a href="javascript:hideLineNums('linenum59')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;class&lt;/span&gt; E1&amp;gt;&lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;    2&lt;/span&gt; Expr&amp;lt;ComplexExpr&amp;lt;Expr&amp;lt;E1&amp;gt;, Expr&amp;lt;Constant&amp;gt;, Multiply&amp;gt; &amp;gt; &lt;span style="color:blue"&gt;operator&lt;/span&gt;* (E1 e1, &lt;span style="color:blue"&gt;double&lt;/span&gt; d) {&lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;    3&lt;/span&gt;     &lt;span style="color:blue"&gt;typedef&lt;/span&gt; ComplexExpr&amp;lt;Expr&amp;lt;E1&amp;gt;, Expr&amp;lt;Constant&amp;gt;, Multiply&amp;gt; ExprType;&lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;    4&lt;/span&gt;     &lt;span style="color:blue"&gt;return&lt;/span&gt; Expr&amp;lt;ExprType&amp;gt;( ExprType(Expr&amp;lt;E1&amp;gt;(e1), Expr&amp;lt;Constant&amp;gt;(Constant(d))) );&lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;    5&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;    6&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;class&lt;/span&gt; E1&amp;gt;&lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;    8&lt;/span&gt; Expr&amp;lt;ComplexExpr&amp;lt;Expr&amp;lt;Constant&amp;gt;, Expr&amp;lt;E1&amp;gt;, Multiply&amp;gt; &amp;gt; &lt;span style="color:blue"&gt;operator&lt;/span&gt;* (&lt;span style="color:blue"&gt;double&lt;/span&gt; d, E1 e1) {&lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;    9&lt;/span&gt;     &lt;span style="color:blue"&gt;typedef&lt;/span&gt; ComplexExpr&amp;lt;Expr&amp;lt;Constant&amp;gt;, Expr&amp;lt;E1&amp;gt;, Multiply&amp;gt; ExprType;&lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;   10&lt;/span&gt;     &lt;span style="color:blue"&gt;return&lt;/span&gt; Expr&amp;lt;ExprType&amp;gt;( ExprType(Expr&amp;lt;Constant&amp;gt;(Constant(d)), Expr&amp;lt;E1&amp;gt;(e1)) );&lt;br /&gt;&lt;span class="linenum59" style="display:none;background:silver"&gt;   11&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;These take care of all cases - except for a small glitch which we can pepare to live with. We can write such expressions as:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;Variable x;&lt;br /&gt;cout &lt;&lt; (x+3.0)(2); // prints 5&lt;br /&gt;cout &lt;&lt; ((x*x+3.0)*(x+3.0))(2); // prints 35&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;but not ones like:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;Variable x;&lt;br /&gt;cout &lt;&lt; (x+3)(2); // prints 5&lt;br /&gt;cout &lt;&lt; ((x*x+3)*(x+3))(2); // prints 35&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If you have to be able to do this, add additional overloads like:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum60" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum60')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum60"&gt;&lt;a href="javascript:hideLineNums('linenum60')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;class&lt;/span&gt; E1&amp;gt;&lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;    2&lt;/span&gt; Expr&amp;lt;ComplexExpr&amp;lt;Expr&amp;lt;E1&amp;gt;, Expr&amp;lt;Constant&amp;gt;, Multiply&amp;gt; &amp;gt; &lt;span style="color:blue"&gt;operator&lt;/span&gt;* (E1 e1, &lt;span style="color:blue"&gt;int&lt;/span&gt; d) {&lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;    3&lt;/span&gt;     &lt;span style="color:blue"&gt;typedef&lt;/span&gt; ComplexExpr&amp;lt;Expr&amp;lt;E1&amp;gt;, Expr&amp;lt;Constant&amp;gt;, Multiply&amp;gt; ExprType;&lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;    4&lt;/span&gt;     &lt;span style="color:blue"&gt;return&lt;/span&gt; Expr&amp;lt;ExprType&amp;gt;( ExprType(Expr&amp;lt;E1&amp;gt;(e1), Expr&amp;lt;Constant&amp;gt;(Constant(d))) );&lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;    5&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;    6&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;class&lt;/span&gt; E1&amp;gt;&lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;    8&lt;/span&gt; Expr&amp;lt;ComplexExpr&amp;lt;Expr&amp;lt;Constant&amp;gt;, Expr&amp;lt;E1&amp;gt;, Multiply&amp;gt; &amp;gt; &lt;span style="color:blue"&gt;operator&lt;/span&gt;* (&lt;span style="color:blue"&gt;int&lt;/span&gt; d, E1 e1) {&lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;    9&lt;/span&gt;     &lt;span style="color:blue"&gt;typedef&lt;/span&gt; ComplexExpr&amp;lt;Expr&amp;lt;Constant&amp;gt;, Expr&amp;lt;E1&amp;gt;, Multiply&amp;gt; ExprType;&lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;   10&lt;/span&gt;     &lt;span style="color:blue"&gt;return&lt;/span&gt; Expr&amp;lt;ExprType&amp;gt;( ExprType(Expr&amp;lt;Constant&amp;gt;(Constant(d)), Expr&amp;lt;E1&amp;gt;(e1)) );&lt;br /&gt;&lt;span class="linenum60" style="display:none;background:silver"&gt;   11&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;As it should be already apparent - there are no objects allocated on free store, no reference counted proxy wrappers, and a fair bit of compile-time type computation and call dispatching - this results in significant savings in the runtime costs of the program, apart from the obvious cut in the number of lines of code. The basic philosophy of the template based program has not changed from the non-template version - it uses functional composition involving the function call operator, and operator overloading to support naturally combining simple expressions into arbitrarily complex expressions. However, the use of templates allows a fair bit of this work to be done at compile time. This is all that is there to Expression templates. You can put together the expression framework using code given here and use the following function to test your code.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum61" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum61')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum61"&gt;&lt;a href="javascript:hideLineNums('linenum61')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; main()&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;    2&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;    3&lt;/span&gt;     Variable x;&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;    4&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;    5&lt;/span&gt;     cout &amp;lt;&amp;lt; ((2.0*x*x + 3.0*x + 3.0)*(2.0*x*x + 3.0*x + 3.0))(2) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;    6&lt;/span&gt;     cout &amp;lt;&amp;lt; integrate((2.0*x*x + 3.0*x + 3.0), 0, 1) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;    7&lt;/span&gt;     cout &amp;lt;&amp;lt; integrate((2.0*x*x + 3.0*x + 3.0)*(2.0*x*x + 3.0*x + 3.0), 0, 1) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;    8&lt;/span&gt;     cout &amp;lt;&amp;lt; integrate((x/(1.0+x)), 0, 1) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;    9&lt;/span&gt;     cout &amp;lt;&amp;lt; integrate(x*x, 0, 7) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;   10&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;   11&lt;/span&gt;     &lt;span style="color:blue"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;   12&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum61" style="display:none;background:silver"&gt;   13&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If you have any trouble understanding and compiling the code, drop me a message.&lt;br /&gt;&lt;br /&gt;&lt;H3&gt;References&lt;/H3&gt;&lt;br /&gt;&lt;OL&gt;&lt;br /&gt;  &lt;LI&gt;Todd Veldhuizen&lt;WBR&gt;&lt;/WBR&gt;: Expression Templates&lt;BR&gt;&lt;A href="http://ubiety.uwaterloo.ca/~tveldhui/papers/Expression-Templates/exprtmpl.html"&gt;http://ubi&lt;WBR&gt;&lt;/WBR&gt;ety.uwater&lt;WBR&gt;&lt;/WBR&gt;loo.ca/~tv&lt;WBR&gt;&lt;/WBR&gt;eldhui/pap&lt;WBR&gt;&lt;/WBR&gt;ers/Expres&lt;WBR&gt;&lt;/WBR&gt;sion-Templ&lt;WBR&gt;&lt;/WBR&gt;ates/exprt&lt;WBR&gt;&lt;/WBR&gt;mpl.html&lt;/A&gt;&lt;br /&gt;  &lt;LI&gt;Expression Templates synopsis on More C++ Idioms&lt;BR&gt;&lt;A href="http://en.wikibooks.org/wiki/More_C++_Idioms/Expression-template"&gt;http://en.&lt;WBR&gt;&lt;/WBR&gt;wikibooks.&lt;WBR&gt;&lt;/WBR&gt;org/wiki/M&lt;WBR&gt;&lt;/WBR&gt;ore_C%2B%2&lt;WBR&gt;&lt;/WBR&gt;B_Idioms/E&lt;WBR&gt;&lt;/WBR&gt;xpression-&lt;WBR&gt;&lt;/WBR&gt;template&lt;/A&gt;&lt;br /&gt;  &lt;LI&gt;Angelika Langer: Expression Templates - Introducti&lt;WBR&gt;&lt;/WBR&gt;on&lt;BR&gt;&lt;A href="http://www.angelikalanger.com/Articles/Cuj/ExpressionTemplates/ExpressionTemplates.htm"&gt;http://www&lt;WBR&gt;&lt;/WBR&gt;.angelikal&lt;WBR&gt;&lt;/WBR&gt;anger.com/&lt;WBR&gt;&lt;/WBR&gt;Articles/C&lt;WBR&gt;&lt;/WBR&gt;uj/Express&lt;WBR&gt;&lt;/WBR&gt;ionTemplat&lt;WBR&gt;&lt;/WBR&gt;es/Express&lt;WBR&gt;&lt;/WBR&gt;ionTemplat&lt;WBR&gt;&lt;/WBR&gt;es.htm&lt;/A&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-3668602590297991459?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/3668602590297991459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=3668602590297991459' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/3668602590297991459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/3668602590297991459'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2008/08/expression-templates-demystified-part-2.html' title='Expression Templates Demystified: Part 2'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-4550799671344127645</id><published>2008-07-31T02:31:00.029+05:30</published><updated>2008-08-04T17:41:49.581+05:30</updated><title type='text'>Expression Templates Demystified</title><content type='html'>&lt;span class="fullpost"&gt;Often in course of our programming work, we want to be able to modify or customize part of a function’s logic at the point of invocation. A very simple example is the C++ STL algorithm std::find_if.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;template&amp;lt;class InputIterator, class Predicate&amp;gt;&lt;br /&gt;InputIterator find_if (InputIterator first, InputIterator last, Predicate pred)&lt;/pre&gt;&lt;br /&gt;Here Predicate represents a functor (an object that overloads the function-call operator). In this particular case, the functor should take an element of the container of which first and last are iterators, and return a boolean truth value based on some criteria, possibly as a function of the element passed.&lt;br /&gt;&lt;br /&gt;Using find_if, one can find all elements in a container, that meet a particular criteria. What’s remarkable is that, what find_if identifies as matching elements in the container is entirely dependent on the logic encoded in the Predicate functor – not on find_if’s implementation. This makes the functionality of find_if open-ended. What we are able to achieve in the process is a form of polymorphism with a very high degree of type-safety and performance. There is a specific name for such idioms – &lt;i&gt;functional composition&lt;/i&gt;. In fact, by combining an arbitrary number of Functors with varied functionality, we can construct a fairly complex piece of functionality. This has a very useful application in developing mini-languages (also called &lt;i&gt;DSEL&lt;/i&gt;s or Domain Specific Embedded Languages - expressions with a syntactic form alien to C++ syntax, being embedded inside C++ programs as valid code. In this article and the &lt;a href="/2008/08/expression-templates-demystified-part-2.html"&gt;next&lt;/a&gt; in this series, we look at the powerful technique called Expression Templates which helps solve these classes of problems.&lt;br /&gt;&lt;br /&gt;We have all learned in high school Calculus course about such functions of single variable as:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;F(x) = x^3 + 3x - 7&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;&lt;i&gt;G(x) = x.sin x + 6&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Representing such simple algebraic functions using functors is not a big hassle. For example, to represent G(x), one could write a functor like the one below:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum31" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum31')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum31"&gt;&lt;a href="javascript:hideLineNums('linenum31')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum31" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; Struct FuncG {&lt;br /&gt;&lt;span class="linenum31" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;(&lt;span style="color:blue;"&gt;double&lt;/span&gt; x) {&lt;br /&gt;&lt;span class="linenum31" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; (x*sin(x) + 6) ;&lt;br /&gt;&lt;span class="linenum31" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum31" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;Imagine you have a Calculus library written in C++ and you have a function called &lt;i&gt;integrate&lt;/i&gt;, that is defined as follows:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum32')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum32"&gt;&lt;a href="javascript:hideLineNums('linenum32')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; Func&amp;gt;&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; integrate(Func f, &lt;span style="color:blue;"&gt;double&lt;/span&gt; low, &lt;span style="color:blue;"&gt;double&lt;/span&gt; high, &lt;span style="color:blue;"&gt;double&lt;/span&gt; epsilon = 0.001) {&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;     assert(low &amp;lt;= high );&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; auc1 = 0, auc2 = 0;&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; running_x = low;&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;     &lt;span style="color:blue;"&gt;while&lt;/span&gt; ( running_x &amp;lt; high ) {&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;         auc1 += f(running_x) * epsilon;&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;         running_x += epsilon;&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;         auc2 += f(running_x) * epsilon;&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; (auc1 + auc2)/2;&lt;br /&gt;&lt;span class="linenum32" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This is essentially an area under the curve calculation that takes the average of upper bound and lower bound sums.&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;Now here is the deal: in course of a mathematical programming one could need to integrate dozens of such mathematical expressions. If there are 50 different expressions to be integrated, a program needs to define 50 different functors – and this is where the efficiency and ease of the system breaks down.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Imagine being able to define function objects that can evaluate arbitrary mathematical expressions and being able to pass such expressions to functions like &lt;i&gt;integrate&lt;/i&gt;. Something like the following:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum33" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum33')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum33"&gt;&lt;a href="javascript:hideLineNums('linenum33')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum33" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; MathFunction f = x*x + 2*x + 3;&lt;br /&gt;&lt;span class="linenum33" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; d = integrate(f, 0, 2);&lt;br /&gt;&lt;span class="linenum33" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; f = x*x*sin*sin + 2*x*cos*sin + cos*cos;&lt;br /&gt;&lt;span class="linenum33" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; PI = 3.1415926535897;&lt;br /&gt;&lt;span class="linenum33" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; d = f(PI/2);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;This almost looks like magic, doesn’t it – but it isn’t. The complex algebraic expression can, in each case, be engineered to give a functor which evaluates the expression for different values of the function argument. &lt;/span&gt;The standard technique or idiom used for enabling such expression building is known as Expression Templates and it is the focus area of this article. But, Expression Templates have a notorious reputation of being difficult to understand and learn and turned off many a learner. Therefore, we would try to build the logic of constructing such expressions intuitively, bit by bit. We will start off with a completely non-template version of the code – an idiom that I discovered for myself and which I call Expression Functor. We would then ‘deduce’ the Expression Template idiom as a special case of this idiom which achieves phenomenal performance improvements and code improvement, using templates.&lt;br /&gt;&lt;br /&gt;So here we go.&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;h3&gt;Breaking down the problem&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;Let us first break the problem into its basic elements. We begin with a simple polynomial expression:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;f(x) = x + 3&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Such an expression has two kinds of entities – a variable (x) and a constant (the literal 3). Both these entities are valid expressions by themselves. For example, the straight line going parallel to the x-axis at a distance of 3 units above x-axis is represented by the function &lt;i&gt;f(x) = 3&lt;/i&gt;. Similarly, the straight line going through the origin at an angle of 45 degrees to both x and y axes is represented by the function &lt;i&gt;f(x) = x&lt;/i&gt;. Modelling these most trivial functions will be our building blocks for non-trivial expression building.&lt;br /&gt;&lt;br /&gt;Consider the case of the horizontal line – &lt;i&gt;f(x) = 3&lt;/i&gt;. We want to model a function which takes any value of x and always returns 3. The basic function would be:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum35" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum35')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum35"&gt;&lt;a href="javascript:hideLineNums('linenum35')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum35" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; f(&lt;span style="color:blue;"&gt;double&lt;/span&gt; x) {&lt;br /&gt;&lt;span class="linenum35" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; 3;&lt;br /&gt;&lt;span class="linenum35" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We want this to become an applicative functor like this:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum36" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum36')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum36"&gt;&lt;a href="javascript:hideLineNums('linenum36')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum36" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; C3 {&lt;br /&gt;&lt;span class="linenum36" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue;"&gt;double&lt;/span&gt;) {&lt;br /&gt;&lt;span class="linenum36" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; 3;&lt;br /&gt;&lt;span class="linenum36" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum36" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;C3 above represents the function &lt;i&gt;f(x) = 3&lt;/i&gt;. Now we want to generalize this function to represent any real number – this is fairly straight-forward:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum34')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum34"&gt;&lt;a href="javascript:hideLineNums('linenum34')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Constant {&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     Constant(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt;&amp;amp; d) : d_(d) {&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue;"&gt;double&lt;/span&gt;) {&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; d_;&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;     &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; d_;&lt;br /&gt;&lt;span class="linenum34" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Such a functor can be used to model any constant expression. C3 would now be equivalent to Constant(3).&lt;br /&gt;&lt;br /&gt;Next, let’s try to model the 45 degree straight line through the origin – &lt;i&gt;f(x) = x&lt;/i&gt;. Turns out that this is even easier – "given any value x, return that very value" should summarize our function. In short:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum37" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum37')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum37"&gt;&lt;a href="javascript:hideLineNums('linenum37')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum37" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Variable {&lt;br /&gt;&lt;span class="linenum37" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue;"&gt;double&lt;/span&gt; x) {&lt;br /&gt;&lt;span class="linenum37" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; x;&lt;br /&gt;&lt;span class="linenum37" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum37" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;These two classes are remarkable on their own, but what happens when we try to model a function like:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;f(x) = x+3&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;What type will be &lt;i&gt;x + 3&lt;/i&gt; – it is not Constant, and it is not an independent variable like Variable either. So we possibly need another class to represent more complex expressions. Come to think of it. Both a Constant and a Variable are each, in themselves, an expression, and so is a more generic expression like &lt;i&gt;x+3&lt;/i&gt;. So it would seem logical that we should have an Expression base class of which, Constant, Variable and other Expression classes can be derived classes. We define it like this:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum38" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum38')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum38"&gt;&lt;a href="javascript:hideLineNums('linenum38')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum38" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Expression {&lt;br /&gt;&lt;span class="linenum38" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;virtual&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue;"&gt;double&lt;/span&gt;) = 0;&lt;br /&gt;&lt;span class="linenum38" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;     &lt;span style="color:blue;"&gt;virtual&lt;/span&gt; Expression* clone() = 0;&lt;br /&gt;&lt;span class="linenum38" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     &lt;span style="color:blue;"&gt;virtual&lt;/span&gt; ~Expression() {&lt;br /&gt;&lt;span class="linenum38" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum38" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;There is a reason for the curious looking &lt;i&gt;Expression* clone()&lt;/i&gt; virtual function. I have included this with the benefit of contrived foresight - having implemented this whole solution already. Without getting ahead of the story, let me tell you that it plays a small but important role in the life cycle management of Expressions. It is used to copy Expressions where needed.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum40')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum40"&gt;&lt;a href="javascript:hideLineNums('linenum40')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Constant : Expression {&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     ...&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;     Expression* clone() {&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt; Constant(*this);&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Variable : Expression {&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;     ...&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;     Expression* clone() {&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt; Variable(*this);&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum40" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We would refer to Expressions like Constant and variable as simple expressions. We also need to define a sub-type of Expression to represent all non-trivial (i.e. other than simple) expressions. But before that can happen, we need to understand how multiple expressions can be combined using arithmetic operators. For example, we want to be able to write such expressions as:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum41" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum41')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum41"&gt;&lt;a href="javascript:hideLineNums('linenum41')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum41" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; Variable x;&lt;br /&gt;&lt;span class="linenum41" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; Expression&amp;amp; e = x*x + 2*x + 1;&lt;br /&gt;&lt;span class="linenum41" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; d = e(5);                  &lt;span style="color:teal;"&gt;// d == 36&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum41" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; d2 = integrate(e, 0, 1);   &lt;span style="color:teal;"&gt;// d2 == 2.33&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Clearly, we need to overload operators like * and + between Variables (x*x), between Constants and Variables (2*x), and between multiple ComplexExpressions (x*x and 2*x). Moreover, the literals like 1 and 2 are not Constant objects, they are doubles that need to be converted to Constant objects. So, these operators should also be overloaded for double arguments. Clearly, given the fact that all of these (Variable, Constant, ComplexExpression) are different sub-types of Expression, it will be easier if we just overload these operators between Expressions, and between Expressions and doubles.&lt;br /&gt;&lt;br /&gt;Now any complex expression can be represented as:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;f(x) = u(x) OP v(x)&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;OP is a binary arithmetic operator. For example:&lt;br /&gt;&lt;br /&gt;f(x) = c is a degenerate case where, u(x) = 1, v(x) = c and OP = *. Or perhaps u(x) = 0, v(x) = c and OP = +. Assuming that we have such an operation available for each appropriate arithmetic operation, we write the class for complex expressions as a template class - the template parameter being the Binary Operation.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum42')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum42"&gt;&lt;a href="javascript:hideLineNums('linenum42')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; Op&amp;gt;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; ComplexExpression : Expression {&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;     Expression* l_;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     Expression* r_;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;     ComplexExpression(Expression&amp;amp; l, Expression&amp;amp; r) : l_(l.clone()), r_(r.clone()) {&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;     ~ComplexExpression() {&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;         &lt;span style="color:blue;"&gt;delete&lt;/span&gt; l_;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;         &lt;span style="color:blue;"&gt;delete&lt;/span&gt; r_;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue;"&gt;double&lt;/span&gt; d) {&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; Op::apply( (*l_)(d), (*r_)(d) );&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;     Expression* clone() {&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt; ComplexExpression(*l_, *r_);&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum42" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The binary operators can be easily defined using simple functors like the following:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum43')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum43"&gt;&lt;a href="javascript:hideLineNums('linenum43')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Add {&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; apply(&lt;span style="color:blue;"&gt;double&lt;/span&gt; l, &lt;span style="color:blue;"&gt;double&lt;/span&gt; r) {&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; l+r;&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Subtract {&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;     &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; apply(&lt;span style="color:blue;"&gt;double&lt;/span&gt; l, &lt;span style="color:blue;"&gt;double&lt;/span&gt; r) {&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; l-r;&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Multiply {&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;     &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; apply(&lt;span style="color:blue;"&gt;double&lt;/span&gt; l, &lt;span style="color:blue;"&gt;double&lt;/span&gt; r) {&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; l*r;&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Divide {&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;     &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; apply(&lt;span style="color:blue;"&gt;double&lt;/span&gt; l, &lt;span style="color:blue;"&gt;double&lt;/span&gt; r) {&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; l/r;&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum43" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Finally, when we combine two simple Expressions, we get a complex expression. For example, x might be a simple expression, but x*x is a complex expression. We want to be able to cascade the operators to any degree, thus:&lt;br /&gt;&lt;br /&gt;x*x*x&lt;br /&gt;&lt;br /&gt;should be a valid expression. Clearly x*x must return an object of such type that can be combined with x using a * operator. x*x must return a reference or pointer to Expression - because Expression is an abstract class so it cannot be returned by value, and besides it is not much point returning it by value because we want to treat it polymorphically (virtual function calls to operator() and clone() in ComplexExpression). Something like the following:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum44" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum44')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum44"&gt;&lt;a href="javascript:hideLineNums('linenum44')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum44" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; Expression* &lt;span style="color:blue;"&gt;operator&lt;/span&gt; * (Expression&amp;amp; l, Expression&amp;amp; r) {&lt;br /&gt;&lt;span class="linenum44" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt; ComplexExpression&amp;lt;Multiply&amp;gt;(l, r);&lt;br /&gt;&lt;span class="linenum44" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The above does not work because operator* returns a pointer to an Expression object - x*x returns a pointer. Consider an expression like x*x*2. x*x*2 is equivalent to (x*x)*2 - since x*x returns an Expression*, we need an operator* which takes a pointer (Expression*) and a double (2). The Standard does not allow an operator to be overloaded on arguments of integer types alone. Thus, operator * can only return a reference.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum45" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum45')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum45"&gt;&lt;a href="javascript:hideLineNums('linenum45')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum45" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; Expression* &lt;span style="color:blue;"&gt;operator&lt;/span&gt; * (Expression&amp;amp; l, Expression&amp;amp; r) {&lt;br /&gt;&lt;span class="linenum45" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt; ComplexExpression&amp;lt;Multiply&amp;gt;(l, r);&lt;br /&gt;&lt;span class="linenum45" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The above works but no one takes the responsibility of deallocating object referred to be the returned reference - we are left with a memory leak.&lt;br /&gt;&lt;br /&gt;What about the following:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum46" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum46')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum46"&gt;&lt;a href="javascript:hideLineNums('linenum46')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum46" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; Expression&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt; * (Expression&amp;amp; l, Expression&amp;amp; r) {&lt;br /&gt;&lt;span class="linenum46" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; ComplexExpression&amp;lt;Multiply&amp;gt;(l, r);&lt;br /&gt;&lt;span class="linenum46" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The above does not even work - because we are passing a reference to a local object created in the function. It has undefined behaviour. What do we do - well, a bit of inevitable complexity creeps in here. We need to return a pointer wrapped in smart wrappers, which can take care of the life-cycles of the underlying pointer and act as proxy objects when participating in mathematical expressions.&lt;br /&gt;&lt;br /&gt;Consider the following definition of a reference counted wrapper cum Proxy for heap allocated Expression pointers:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum47')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum47"&gt;&lt;a href="javascript:hideLineNums('linenum47')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; ExpressionRef {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     ExpressionRef(Expression* ptr) : sp_(ptr), ref_cnt_(1) {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt;     ~ExpressionRef() {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;         &lt;span style="color:blue;"&gt;if&lt;/span&gt; (--ref_cnt_ == 0) {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;             &lt;span style="color:blue;"&gt;delete&lt;/span&gt; sp_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;             sp_ = 0;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;         }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;     ExpressionRef(&lt;span style="color:blue;"&gt;const&lt;/span&gt; ExpressionRef&amp;amp; source) : sp_(source.sp_) {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;         ++ref_cnt_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;     &lt;span style="color:blue;"&gt;inline&lt;/span&gt; ExpressionRef&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt; = (&lt;span style="color:blue;"&gt;const&lt;/span&gt; ExpressionRef&amp;amp; rhs) {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;         &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( this != &amp;amp;rhs ) {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;             &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( --ref_cnt_ &amp;lt;= 0 ) {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;                 &lt;span style="color:blue;"&gt;delete&lt;/span&gt; sp_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;                 sp_ = 0;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;             }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;             sp_ = rhs.sp_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;             ++ref_cnt_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt;         }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; *this;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;() (&lt;span style="color:blue;"&gt;double&lt;/span&gt; d) {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; (*sp_)(d);&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   34&lt;/span&gt;     ExpressionRef clone() {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   35&lt;/span&gt;         ExpressionRef copy(sp_-&amp;gt;clone());&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   36&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; copy;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   37&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   38&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   39&lt;/span&gt;     &lt;span style="color:blue;"&gt;inline&lt;/span&gt; Expression* get() {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   40&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; sp_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   41&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   42&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   43&lt;/span&gt;     &lt;span style="color:blue;"&gt;inline&lt;/span&gt; Expression&amp;amp; getref() {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   44&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; *sp_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   45&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   46&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   47&lt;/span&gt;     &lt;span style="color:blue;"&gt;inline&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt;*() {&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   48&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; sp_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   49&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   50&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   51&lt;/span&gt; &lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   52&lt;/span&gt;     Expression *sp_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   53&lt;/span&gt;     &lt;span style="color:blue;"&gt;int&lt;/span&gt; ref_cnt_;&lt;br /&gt;&lt;span class="linenum47" style="DISPLAY: none; BACKGROUND: silver"&gt;   54&lt;/span&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using this class, we can rewrite operator* like below:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum48" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum48')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum48"&gt;&lt;a href="javascript:hideLineNums('linenum48')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum48" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; ExpressionRef &lt;span style="color:blue;"&gt;operator&lt;/span&gt; * (Expression&amp;amp; l, Expression&amp;amp; r) {&lt;br /&gt;&lt;span class="linenum48" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; ExpressionRef(&lt;span style="color:blue;"&gt;new&lt;/span&gt; ComplexExpression&amp;lt;Multiply&amp;gt;(l, r));&lt;br /&gt;&lt;span class="linenum48" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum48" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Thus, the return value of x*x is of type ExpressionRef, and at least it wraps a polymorphic reference to Expression, and ensure that there would be no memory leaks. The only problem now is to make expressions such as x*x*x valid. x*x*x translates to (x*x)*x - whereby x*x returns ExpressionRef and x is some sub-type Expression. This is not such a big deal - we define operator * (ExpressionRef&amp;amp;, Expression&amp;amp;). We also need the alternative permutations: operator * (Expression&amp;amp;, ExpressionRef&amp;amp;). In fact, to allow expressions such as (x*x)*(x*x), one must also allow operators like: operator * (ExpressionRef&amp;amp;, ExpressionRef&amp;amp;).&lt;br /&gt;&lt;br /&gt;At this point, all the issues with our solution are resolved - and we need to take stock of the final set of operator overloads we need to make the expressions work naturally. Here is a summary:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Expression&amp;amp; Op Expression&amp;amp;&lt;br /&gt;ExpressionRef&amp;amp; Op ExpressionRef&amp;amp;&lt;br /&gt;Expression&amp;amp; Op double - and reverse permutation&lt;br /&gt;ExpressionRef&amp;amp; Op double - and reverse permutation&lt;br /&gt;ExpressionRef&amp;amp; Op Expression&amp;amp; - and reverse permutation&lt;br /&gt;&lt;br /&gt;That makes it 8 types of signatures. For each type, if we plan to implement +, -, * and /, then we will have a total of 32 operators. One set of operators can have real implementations and the rest can be defined in terms of the first set. Here are the definitions for the + operators.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum49')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum49"&gt;&lt;a href="javascript:hideLineNums('linenum49')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; ExpressionRef &lt;span style="color:blue;"&gt;operator&lt;/span&gt; + (Expression&amp;amp; l, Expression&amp;amp; r) {&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; ExpressionRef(&lt;span style="color:blue;"&gt;new&lt;/span&gt; ComplexExpression&amp;lt;Add&amp;gt;(l, r));&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; ExpressionRef &lt;span style="color:blue;"&gt;operator&lt;/span&gt; + (ExpressionRef&amp;amp; l, ExpressionRef&amp;amp; r) {&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; l.getref() + r.getref();&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt; &lt;span style="color:teal;"&gt;///////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt; ExpressionRef &lt;span style="color:blue;"&gt;operator&lt;/span&gt; + (Expression&amp;amp; l, &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; d) {&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; l + Constant(d);&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt; ExpressionRef &lt;span style="color:blue;"&gt;operator&lt;/span&gt; + (&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; d, Expression&amp;amp; l) {&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; Constant(d) + l;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt; &lt;span style="color:teal;"&gt;//////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt; ExpressionRef &lt;span style="color:blue;"&gt;operator&lt;/span&gt; + (ExpressionRef&amp;amp; l, Expression&amp;amp; r) {&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; l.getref() + r;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt; ExpressionRef &lt;span style="color:blue;"&gt;operator&lt;/span&gt; + (Expression&amp;amp; l, ExpressionRef&amp;amp; r) {&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; l + r.getref();&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt; &lt;span style="color:teal;"&gt;//////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt; ExpressionRef &lt;span style="color:blue;"&gt;operator&lt;/span&gt; + (ExpressionRef&amp;amp; l, &lt;span style="color:blue;"&gt;double&lt;/span&gt; d) {&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; l.getref() + Constant(d);&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   34&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   35&lt;/span&gt; ExpressionRef &lt;span style="color:blue;"&gt;operator&lt;/span&gt; + (&lt;span style="color:blue;"&gt;double&lt;/span&gt; d, ExpressionRef&amp;amp; r) {&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   36&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; Constant(d) + r.getref();&lt;br /&gt;&lt;span class="linenum49" style="DISPLAY: none; BACKGROUND: silver"&gt;   37&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To test this solution, use the following functions:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum50')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum50"&gt;&lt;a href="javascript:hideLineNums('linenum50')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; integrate(Expression&amp;amp; e, &lt;span style="color:blue;"&gt;double&lt;/span&gt; low=0, &lt;span style="color:blue;"&gt;double&lt;/span&gt; high = 1, &lt;span style="color:blue;"&gt;double&lt;/span&gt; epsilon = 0.001) {&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     assert(low &amp;lt;= high );&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; auc1 = 0, auc2 = 0;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; running_x = low;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt;     &lt;span style="color:blue;"&gt;while&lt;/span&gt; ( running_x &amp;lt; high ) {&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;         auc1 += e(running_x) * epsilon;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;         running_x += epsilon;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;         auc2 += e(running_x) * epsilon;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; (auc1 + auc2)/2;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; integrate(ExpressionRef&amp;amp; er, &lt;span style="color:blue;"&gt;double&lt;/span&gt; low=0, &lt;span style="color:blue;"&gt;double&lt;/span&gt; high = 1, &lt;span style="color:blue;"&gt;double&lt;/span&gt; epsilon = 0.001) {&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; integrate(er.getref(), low, high, epsilon);&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main()&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;     Variable x;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;     cout &amp;lt;&amp;lt; ((2*x*x + 3*x + 3)*(2*x*x + 3*x + 3))(2) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;     cout &amp;lt;&amp;lt; integrate((2*x*x + 3*x + 3), 0, 1) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;     cout &amp;lt;&amp;lt; integrate((2*x*x + 3*x + 3)*(2*x*x + 3*x + 3), 0, 1) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt;     cout &amp;lt;&amp;lt; integrate((x/(1+x)), 0, 1) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt;     cout &amp;lt;&amp;lt; integrate(Exp(), 0, 1) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt;     cout &amp;lt;&amp;lt; integrate(x*x, 0, 7) &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;span class="linenum50" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Having come thus far, you may have made two observations. One we have written code which is nifty, and works well but is repetitive in some parts and not very extensible. Second, although this article is about Expression Templates, we have hardly used any templates (except for the ComplexExpression class).&lt;br /&gt;&lt;br /&gt;What are the key concepts in this code which enabled creating the Expression Functors?&lt;br /&gt;1. Nesting function objects and operator() of outer objects calling operator()'s of inner objects - for example ComplexExpression::operator(). The ormal term for these idioms is &lt;i&gt;Functional Composition&lt;/i&gt;.&lt;br /&gt;2. Overloading operators on Expression types.&lt;br /&gt;&lt;br /&gt;What we've done so far was laying the foundation for our understanding of Expression Templates. The real deal should not take a lot of time after this. We will now head into investigating how this model of functional composition can be retained and generalized, performance of the Expressions phenomenally improved, and the lines of code drastically cut - by using Templates. This is the topic of the &lt;a href="/2008/08/expression-templates-demystified-part-2.html"&gt;next part&lt;/a&gt; of this article.&lt;br&gt;&lt;br /&gt;&lt;H3&gt;References&lt;/H3&gt;&lt;br /&gt;&lt;OL&gt;&lt;LI&gt;Todd Veldhuizen&lt;WBR&gt;&lt;/WBR&gt;: Expression Templates&lt;BR&gt;&lt;br /&gt;  &lt;A href="http://ubiety.uwaterloo.ca/~tveldhui/papers/Expression-Templates/exprtmpl.html"&gt;http://ubi&lt;WBR&gt;&lt;/WBR&gt;ety.uwater&lt;WBR&gt;&lt;/WBR&gt;loo.ca/~tv&lt;WBR&gt;&lt;/WBR&gt;eldhui/pap&lt;WBR&gt;&lt;/WBR&gt;ers/Expres&lt;WBR&gt;&lt;/WBR&gt;sion-Templ&lt;WBR&gt;&lt;/WBR&gt;ates/exprt&lt;WBR&gt;&lt;/WBR&gt;mpl.html&lt;/A&gt;&lt;br /&gt;&lt;/OL&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-4550799671344127645?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/4550799671344127645/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=4550799671344127645' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/4550799671344127645'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/4550799671344127645'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2008/07/expression-templates-demystified.html' title='Expression Templates Demystified'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-6269474965255702309</id><published>2008-07-12T18:54:00.081+05:30</published><updated>2008-09-07T02:41:53.815+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='introduction'/><category scheme='http://www.blogger.com/atom/ns#' term='Boost libraries'/><category scheme='http://www.blogger.com/atom/ns#' term='howto'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Getting Started with Boost</title><content type='html'>&lt;h3&gt;Ten nifty tricks to incorporate into your armoury in your first week of using Boost&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;Boost has a wide array of libraries, and they often differ in complexity, expanse and sometimes power, but not in usefulness. Obviously a utility to convert strings to integers will not have the same complexity or power as a library for functional compositions. But both are useful in their own right. Today, Boost has close to 40 libraries (may be more) and this large number, along with the wide variety therein, sometimes serves to overawe the new learner. This article has a specific purpose - to get you started with a few, relatively easy, nifty tricks that you can employ in many general cases to add value and usability to your code, without having to be a Boost expert. Such head-starts serve to increase our motivation to use a tool, exposing us to the first hand benefits of a tool at the expense of a little effort. So without further ado, let me cut to the chase.&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;h4&gt;1. Time measurements and thermometer bars&lt;/h4&gt;&lt;br /&gt;The following piece of code illustrates how to use the Boost.Timer library to measure elapsed time between two points in code. Using this facility, you can easily measure the time taken to execute a certain section of code. To view the sorce code with numbered lines, click "Show line numbers".&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum1')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum1"&gt;&lt;a href="javascript:hideLineNums('linenum1')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;sstream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/progress.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; boost::progress_timer;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::stringstream;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main()&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;     stringstream os;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; elapsed_time = 0.0;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;     {&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;         progress_timer t(os);&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;         &lt;span style="color:blue;"&gt;int&lt;/span&gt; arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;         &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; arr_elems = &lt;span style="color:blue;"&gt;sizeof&lt;/span&gt;(arr)/&lt;span style="color:blue;"&gt;sizeof&lt;/span&gt;(&lt;span style="color:blue;"&gt;int&lt;/span&gt;);&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;         &lt;span style="color:blue;"&gt;for&lt;/span&gt; ( &lt;span style="color:blue;"&gt;int&lt;/span&gt; i=0; i&amp;lt;arr_elems; i++ ) {&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;             Sleep(3000);&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;         }&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;     &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( os &amp;gt;&amp;gt; elapsed_time ) {&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt;         cout &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Net elapsed time: "&lt;/span&gt; &amp;lt;&amp;lt; elapsed_time &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt;     } &lt;span style="color:blue;"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt;         cout &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Net elapsed time: "&lt;/span&gt; &amp;lt;&amp;lt; os.str() &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;span class="linenum1" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The Boost header we used for our purpose is progress.hpp. We create an array of 16 integers and simulate some processing on this array by iterating through the elements of the array and pausing for 3 seconds for each element to simulate a period of processing (line 20). We intend to find the time taken to set up the array and then perform this dummy operation. To do this, we enclose the relevant part of the code (lines 14 through 22) in braces to define a scope. At the start of the scope, we declare an object of boost::progress_timer and initialize it with a stream object which can be written to. At the end of the scope, the progress timer object will write the total time elapsed to the stream object. Thereafter we either extract the double precision floating point value of the time elapsed in seconds from the stream (line 24) or directly write the stream's contents to the standard output (line 27).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;2. Tokenizing strings and seamless value conversions&lt;/h4&gt;&lt;br /&gt;Suppose you are working on a banking back-office system and are scrubbing the summary data from your bank's daily closing report. Now this report is in some delimited format which your report rendering software understands, but which is not XML or something. Your requirement is simple - you want to extract all the data from the report and do some processing on it. In other words, you need to parse the report data. Second, there are numeric items in the list as well and you want to do quick comparisons of some of those - like the age of the account holder and the net balance in her account. All these quantities will be read as strings from the input so we will also need an effective and consistent way to convert these to numeric values. Parsing data of even moderate complexity can be a tough job and while the standard library containers and string classes provide adequate tools for doing this job, the code you'd end up with often tends to be long and not very readable. And converting string to numeric types isn't always straightforward either. On the other hand you don't want to spend more than half-hour in writing the code. What do you do. Well, you use the Boost.Tokenizer and Boost.Conversion libraries and you are through.&lt;br /&gt;&lt;br /&gt;Suppose the data comes to you in the form of several records delimited inside braces ( { and } ). Within each record, several fields are delimited by semi-colons (;) and each field is a name-value pair of the form name=value or name:value. You don't want to be bothered about verifying the correctness of the grammar - you could assume that the document is well-formed and you then want to extract the data. Here is some sample data.&lt;br /&gt;&lt;p&gt;{name:Chaim Rabinowitz;acc:5629311372;bank:Citibank N.A.;branch:Tel-Aviv;age:69;balance:100000.00}{name:Mordechai Silberman;acc:46268675539;bank:Carmel Bank;branch:Haifa;age:43;balance:20000.00}{name:Amir Belleli;acc:5538639473;bank:Carmel Bank;branch:Nahariyya;age:54;balance:64000.00}{name:Liat Gershgoren;acc:5538639473;bank:The Hebrew Bank;branch:K'far Etzion;age:27;balance:28000.00}&lt;/p&gt;&lt;br /&gt;And here is the code.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum2')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum2"&gt;&lt;a href="javascript:hideLineNums('linenum2')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/tokenizer.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/algorithm/string.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/lexical_cast.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::string;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::vector;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; boost::char_separator;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; boost::split;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; boost::lexical_cast;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; AccountSummary {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;     string name;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;     &lt;span style="color:blue;"&gt;int&lt;/span&gt; age;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;     string bank;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;     string branch;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;     string account_number;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; balance;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;     AccountSummary() : age(0), balance(0.0) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main()&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt;     string str = &lt;span style="color:red;"&gt;"{name:Chaim Rabinowitz;acc:5629311372;bank:Citibank N.A.;branch:Tel-Aviv;age:69;balance:100000.00}"&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;                  &lt;span style="color:red;"&gt;"{name:Mordechai Silberman;acc:46268675539;bank:Carmel Bank;branch:Haifa;age:43;balance:20000.00}"&lt;/span&gt;;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt;     &lt;span style="color:blue;"&gt;typedef&lt;/span&gt; boost::tokenizer&amp;lt;char_separator&amp;lt;&lt;span style="color:blue;"&gt;char&lt;/span&gt;&amp;gt; &amp;gt; tokenizer;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt;     char_separator&amp;lt;&lt;span style="color:blue;"&gt;char&lt;/span&gt;&amp;gt; sep(&lt;span style="color:red;"&gt;"{}"&lt;/span&gt;);&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   34&lt;/span&gt;     tokenizer records(str, sep);&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   35&lt;/span&gt;     vector&amp;lt;AccountSummary&amp;gt; account_data;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   36&lt;/span&gt;     &lt;span style="color:blue;"&gt;bool&lt;/span&gt; bad_data = &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   37&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   38&lt;/span&gt;     &lt;span style="color:blue;"&gt;for&lt;/span&gt; (tokenizer::iterator tok_iter = records.begin(); tok_iter != records.end(); ++tok_iter) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   39&lt;/span&gt;         string next_record = *tok_iter;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   40&lt;/span&gt;         AccountSummary next_acc_data;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   41&lt;/span&gt;         char_separator&amp;lt;&lt;span style="color:blue;"&gt;char&lt;/span&gt;&amp;gt; sep2(&lt;span style="color:red;"&gt;";"&lt;/span&gt;);&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   42&lt;/span&gt;         tokenizer fields(next_record, sep2);&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   43&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   44&lt;/span&gt;         &lt;span style="color:blue;"&gt;try&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   45&lt;/span&gt;             &lt;span style="color:blue;"&gt;for&lt;/span&gt; (tokenizer::iterator fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   46&lt;/span&gt;                 string next_field = *fld_iter;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   47&lt;/span&gt;                 vector&amp;lt;string&amp;gt; key_val;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   48&lt;/span&gt;                 split(key_val, next_field, boost::is_any_of(&lt;span style="color:red;"&gt;":="&lt;/span&gt;));&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   49&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   50&lt;/span&gt;                 &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( key_val.size() &amp;gt;= 2 ) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   51&lt;/span&gt;                     &lt;span style="color:blue;"&gt;const&lt;/span&gt; string&amp;amp; key = key_val[0];&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   52&lt;/span&gt;                     &lt;span style="color:blue;"&gt;const&lt;/span&gt; string&amp;amp; value = key_val[1];&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   53&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   54&lt;/span&gt;                     &lt;span style="color:blue;"&gt;if&lt;/span&gt; (key == &lt;span style="color:red;"&gt;"name"&lt;/span&gt;) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   55&lt;/span&gt;                         next_acc_data.name = value;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   56&lt;/span&gt;                     } &lt;span style="color:blue;"&gt;else&lt;/span&gt; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (key == &lt;span style="color:red;"&gt;"acc"&lt;/span&gt;) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   57&lt;/span&gt;                         next_acc_data.account_number = value;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   58&lt;/span&gt;                     } &lt;span style="color:blue;"&gt;else&lt;/span&gt; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (key == &lt;span style="color:red;"&gt;"bank"&lt;/span&gt;) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   59&lt;/span&gt;                         next_acc_data.bank = value;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   60&lt;/span&gt;                     } &lt;span style="color:blue;"&gt;else&lt;/span&gt; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (key == &lt;span style="color:red;"&gt;"branch"&lt;/span&gt;) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   61&lt;/span&gt;                         next_acc_data.branch = value;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   62&lt;/span&gt;                     } &lt;span style="color:blue;"&gt;else&lt;/span&gt; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (key == &lt;span style="color:red;"&gt;"age"&lt;/span&gt;) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   63&lt;/span&gt;                         next_acc_data.age = lexical_cast&amp;lt;&lt;span style="color:blue;"&gt;int&lt;/span&gt;&amp;gt;(value);&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   64&lt;/span&gt;                     } &lt;span style="color:blue;"&gt;else&lt;/span&gt; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (key == &lt;span style="color:red;"&gt;"balance"&lt;/span&gt;) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   65&lt;/span&gt;                         next_acc_data.balance = lexical_cast&amp;lt;&lt;span style="color:blue;"&gt;double&lt;/span&gt;&amp;gt;(value);&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   66&lt;/span&gt;                     } &lt;span style="color:blue;"&gt;else&lt;/span&gt; { &lt;span style="color:teal;"&gt;// Do nothing&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   67&lt;/span&gt;                     }&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   68&lt;/span&gt;                 }&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   69&lt;/span&gt;             }&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   70&lt;/span&gt;         }  &lt;span style="color:blue;"&gt;catch&lt;/span&gt;(boost::bad_lexical_cast&amp;amp; be) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   71&lt;/span&gt;             bad_data = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   72&lt;/span&gt;         }&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   73&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   74&lt;/span&gt;         &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( bad_data ) {&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   75&lt;/span&gt;             bad_data = &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   76&lt;/span&gt;             &lt;span style="color:blue;"&gt;continue&lt;/span&gt;;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   77&lt;/span&gt;         }&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   78&lt;/span&gt;         account_data.push_back(next_acc_data);&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   79&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   80&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   81&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; EXIT_SUCCESS;&lt;br /&gt;&lt;span class="linenum2" style="DISPLAY: none; BACKGROUND: silver"&gt;   82&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Obviously, this code does a few things with the data, but certainly does not print it. For a nice way to print this data in a suitable format, look at section 4 below.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;3. Expressive code with zero resource-leaks&lt;/h4&gt;&lt;br /&gt;In an ideal world, all libraries and APIs you work with will have perfect semantics and you will never need to make compromises of any kind in your code. In such a world, you would only need to work with first class C++ objects which take care of their own initialization and clean-up in the C++ tradition of construction and destruction. In the real world however, you would be writing C++ applications to talk to legacy systems using a C API that is less than ideal. In that real world, you will have strdup-style C APIs that return dynamically allocated storage to you and expect you to take care of the life cycle of the pointer, remembering to call free on the pointer once you are done. As a real life programmer you would surely face such dilemmas several times, in several forms - and that's where the family of Smart pointers comes into the picture.&lt;br /&gt;&lt;br /&gt;Smart pointers are simple classes which can "wrap" pointers to objects of different kinds, and in some cases arrays of objects. The primary advantage in the use of such wrappers is the seamless life-cycle management of these pointers (through the RAII idiom) and exception-safety. &lt;strong&gt;In fact the Smart Pointer collection from Boost is so good that you no more have an excuse to write C++ code that has memory leaks.&lt;/strong&gt; I am not about to start a discussion on either the &lt;a href="http://sourceforge.net/docman/display_doc.php?docid=8673&amp;amp;group_id=9028"&gt;RAII-idiom&lt;/a&gt; or &lt;a href="http://www.boost.org/community/exception_safety.html"&gt;exception-safety&lt;/a&gt; - primarily because of lack of space in this column, and sites that handle the topics with more focus. Here we will see how the use of Boost's smart pointers provide power, simplicity and correct, reliable behaviour to your code.&lt;br /&gt;&lt;br /&gt;Consider the following piece of code - let me tell you, it does not compile.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum3')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum3"&gt;&lt;a href="javascript:hideLineNums('linenum3')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;cctype&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::string;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt; &lt;span style="color:blue;"&gt;extern&lt;/span&gt; &lt;span style="color:red;"&gt;"C"&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; strucase(&lt;span style="color:blue;"&gt;char&lt;/span&gt; *str);&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; strucase(&lt;span style="color:blue;"&gt;char&lt;/span&gt; *str) {&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;     &lt;span style="color:blue;"&gt;int&lt;/span&gt; count = 0;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;     &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( str ) {&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;         &lt;span style="color:blue;"&gt;do&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;             (islower(*str)) ? (*str=toupper(*str), count++) : 0;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;         } &lt;span style="color:blue;"&gt;while&lt;/span&gt;(*str++);&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; count;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt; string new_api() {&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:red;"&gt;"Eddie Cochran"&lt;/span&gt;;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main()&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt;     string str = new_api();&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;     strucase(str.c_str());&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;span class="linenum3" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Given a C++ string, if you want to pass it's contents as a char* to a function that takes a (non-const) char* rather than a const char* (like strucase above), you &lt;i&gt;have to&lt;/i&gt; make a copy of the string's underlying character array into a new array, and pass the base address of the array instead. It's a bit of a fib when I say you have to - what good is const_cast for after all? But then, what guarantee do you have that this strucase function that takes a &lt;i&gt;non-const&lt;/i&gt; char* does not write to the string you passed. In fact, in this case strucase does write to the string, converting it in place to an all upper-case ASCII string. So the call at line 30 above fails (click Show line numbers to see line numbers). The string returned by new_api can be of arbitrary length (not exceeding platform specific limits, still) so you cannot create an array, with a size known at compile time, to hold the copy of the string. In other words, you have to allocate memory dynamically, and whenever you do that there is the hassle of correctly deallocating the allocated memory once every part of your code is done with using it. The following code addresses the issue with a smart array from Boost.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum4')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum4"&gt;&lt;a href="javascript:hideLineNums('linenum4')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;cstring&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/scoped_array.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;cctype&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::string;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; boost::scoped_array;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt; &lt;span style="color:blue;"&gt;extern&lt;/span&gt; &lt;span style="color:red;"&gt;"C"&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; strucase(&lt;span style="color:blue;"&gt;char&lt;/span&gt; *str);&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; strucase(&lt;span style="color:blue;"&gt;char&lt;/span&gt; *str) {&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;     &lt;span style="color:blue;"&gt;int&lt;/span&gt; count = 0;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;     &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( str ) {&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;         &lt;span style="color:blue;"&gt;do&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;             (islower(*str)) ? (*str=toupper(*str), count++) : 0;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;         } &lt;span style="color:blue;"&gt;while&lt;/span&gt;(*str++);&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; count;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt; string new_api() {&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:red;"&gt;"Eddie Cochran"&lt;/span&gt;;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main() {&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;     string str = new_api();&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt;     scoped_array&lt;char&gt; sa( new char[str.length()+1] );&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt;     &lt;span style="color:blue;"&gt;char&lt;/span&gt; *copied_str = sa.get();&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt;     strncpy(copied_str, str.c_str(), str.length());&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   34&lt;/span&gt;     copied_str[str.length()] = 0;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   35&lt;/span&gt;     strucase(copied_str);&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   36&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   37&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;span class="linenum4" style="DISPLAY: none; BACKGROUND: silver"&gt;   38&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The first good thing about the above code is that it ensures there is no memory leak - the array allocated on line 31 is wrapped inside the scoped array object &lt;i&gt;sa&lt;/i&gt; and at the end of the enclosing scope (in this case the main function), the destructor of scoped array kicks in and deallocates the entire array by calling delete[] on it.&lt;br /&gt;&lt;br /&gt;Now imagine that instead of all this code being written inside the main() function, it was a different function which carried out these operations returned the allocated character array. Something like the following will obviously crash your code:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum5')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum5"&gt;&lt;a href="javascript:hideLineNums('linenum5')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;char*&lt;/span&gt; my_new_string() {&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     string str = new_api();&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;     scoped_array&lt;char&gt; sa( new char[str.length()+1] );&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     &lt;span style="color:blue;"&gt;char&lt;/span&gt; *copied_str = sa.get();&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt;     strncpy(copied_str, str.c_str(), str.length());&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;     copied_str[str.length()] = 0;&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;     strucase(copied_str);&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; copied_str;&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt; int main() {&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;     char * new_str = my_new_string();&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;     cout &amp;lt;&amp;lt; new_str &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;     char c='a';&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;     new_str[0] = c;&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Lines 14 and 16 are recipes for disaster because new_str is a copy of the pointer copied_str that the my_new_string() function passes back to you. And both these are aliases or copies of the underlying pointer in the scoped_array in my_new_string(). As soon as my_new_string() returns, scoped_array makes sure that the memory pointed to by its underlying pointer is deallocated. So in the main() function, you are dealing with an invalid memory reference - new_str.&lt;br /&gt;&lt;br /&gt;What do we do? Pass back a copy of the scoped_array object itself? For one, scoped_array cannot be copied. Even if it could be, whether that would work or not would depend on whether or not scoped_array detects copying and such replication operations and keeps a count of the places in the code that have valid references to the object. This is called reference counting and scoped_array does not do it. Boost shared_array does. All you need to change in the above code is this:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum5" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum6')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum5"&gt;&lt;a href="javascript:hideLineNums('linenum6')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; shared_array&amp;lt;&lt;span style="color:blue;"&gt;char&lt;/span&gt;&amp;gt; my_new_string() {&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt;     string str = new_api();&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt;     shared_array&lt;char&gt; sa( new char[str.length()+1] );&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;     &lt;span style="color:blue;"&gt;char&lt;/span&gt; *copied_str = sa.get();&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt;     strncpy(copied_str, str.c_str(), str.length());&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;     copied_str[str.length()] = 0;&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;     strucase(copied_str);&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; sa;&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main() {&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;     shared_array&amp;lt;&lt;span style="color:blue;"&gt;char&lt;/span&gt;&amp;gt; sa = my_new_string();&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;     &lt;span style="color:blue;"&gt;char&lt;/span&gt; *new_str = sa.get();&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;     cout &amp;lt;&amp;lt; new_str &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;     &lt;span style="color:blue;"&gt;char&lt;/span&gt; c='a';&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;     new_str[0] = c;&lt;br /&gt;&lt;span class="linenum6" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, whenever you need to pass around a dynamically allocated array, just pass around the shared_array wrapper for it. It takes care of everything else.&lt;br /&gt;&lt;br /&gt;There are more goodies in the Boost Smart Pointer library, including its work-horse - shared_ptr. In fact having already shown scoped_array and shared_array, it is not much work now to get up and running with scoped_ptr and shared_ptr. Both scoped_ptr and shared_ptr are capable of encapsulating a pointer to a single object on dynamically allocated storage. scoped_ptr is, of course, not copiable and not reference counted - suitable for operations limited in a single scope. shared_ptr on the other hand is a remarkable mobile smart pointer - you can freely copy and pass it around, it is fully reference counted. These classes overload the -&gt; and * operators and therefore working with them is a breeze. Here is a short example to wrap up this section.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum7')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum7"&gt;&lt;a href="javascript:hideLineNums('linenum7')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/shared_ptr.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::string;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; boost::shared_ptr;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Foo {&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;     &lt;span style="color:blue;"&gt;void&lt;/span&gt; bar() {}  &lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt; &lt;span style="color:blue;"&gt;typedef&lt;/span&gt; shared_ptr&amp;lt;Foo&amp;gt; FooPtr;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt; FooPtr createFoo() {&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;     FooPtr sp(&lt;span style="color:blue;"&gt;new&lt;/span&gt; Foo);&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; sp;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt; }&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main() {&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;     FooPtr sp = createFoo();&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;     sp-&amp;gt;bar();&lt;br /&gt;&lt;span class="linenum7" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Imagine how you would have handled the case where you could need to create arbitrary objects on the free-store with new and pass them around in the program, across scopes, sharing between multiple owning objects and scopes. Without the shared_ptr, such brevity was impossible.&lt;br /&gt;&lt;br /&gt;In reality, shared_ptr models a "shared ownership" idiom that concerns managing the life cycle of the underlying heap-object. Sometimes, this is precisely what we want to avoid - ownership. In such cases, shared_ptr fails to prevent memory leaks and we need other alternatives. For more details, see &lt;a href="http://www.blogger.com/2008/08/aliasing-pointers-using-weakness-to.html"&gt;weak_ptr's&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;4. Using reference wrappers to write efficient code&lt;/h4&gt;&lt;br /&gt;Suppose you are dealing with objects of a class which cannot be copied, and perhaps not be assigned either. May be you are modelling humans in a social group and one of the assumptions / constraints is that humans cannot be replicated or cloned. So a Human object would essentially be a non-assignable, non-copiable object. And yet, for the purposes of effectively managing large groups of humans, you have to use different kinds of containers. May be you are modelling the behaviour of humans in a queue and you choose an std::list here - because the Humans have the liberty to leave the queue at any point. A list perhaps also makes sense, because a Human might opportunistically join a queue at the middle rather than at the end where he is actually supposed to join - to reduce his personal wait time. Whatever the social engineering issue here, we have a bigger computer programming issue to beat. Humans are not copiable or assignable - but an std::list container requires its elements to be both. You have no choice - either make the Humans copiable (an untenable model) or roll out your own containers which can store object references - or so you sulk. Enter Boost reference wrappers - a feature that should actually make it to the next release of the C++ Language and Standard Library (C++0x) - and you have reason to cheer.&lt;br /&gt;&lt;br /&gt;The idea behind reference wrappers is simple. Create a trivial wrapper around a reference to an object and make this wrapper copiable and assignable. This wrapper object stores a reference or pointer to the real object. The wrapper object can be easily copied and assigned - without the underlying object getting replicated. Finally, the wrapper object should transparently degrade to the underlying type in contexts where an object of the underlying type is needed. This makes a reference wrapped object very easy to store in STL containers and easy to use in STL algorithms. The boost::reference_wrapper&amp;lt;T&amp;gt; template conforms to all these requirements and does a great job of it. Consider the following code.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum9')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum9"&gt;&lt;a href="javascript:hideLineNums('linenum9')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/ref.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Human {&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;     Human() : id_(++count) {&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;     &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; id() &lt;span style="color:blue;"&gt;const&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; id_;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt; &lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;     &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; id_;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;     Human(&lt;span style="color:blue;"&gt;const&lt;/span&gt; Human&amp;amp; srt);&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;     Human&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt; = (&lt;span style="color:blue;"&gt;const&lt;/span&gt; Human&amp;amp; srt);&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;     &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; count;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; Human::count = 0;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt; &lt;span style="color:blue;"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; T&amp;gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; PrintId {&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt;     &lt;span style="color:blue;"&gt;int&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;()(T t) {&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt;         cout &amp;lt;&amp;lt; t.id() &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   34&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main() {&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   35&lt;/span&gt;     &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::vector;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   36&lt;/span&gt;     Human h1, h2, h3, h4;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   37&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   38&lt;/span&gt;     vector&amp;lt; boost::reference_wrapper&amp;lt;Human &lt;span style="color:blue;"&gt;const&lt;/span&gt;&amp;gt; &amp;gt; people;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   39&lt;/span&gt;     people.reserve(4);&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   40&lt;/span&gt;     people.push_back(boost::cref(h1));&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   41&lt;/span&gt;     people.push_back(boost::cref(h2));&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   42&lt;/span&gt;     people.push_back(boost::cref(h3));&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   43&lt;/span&gt;     people.push_back(boost::cref(h4));&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   44&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   45&lt;/span&gt;     std::for_each(people.begin(), people.end(), PrintId&amp;lt;Human &lt;span style="color:blue;"&gt;const&lt;/span&gt;&amp;amp;&amp;gt;());&lt;br /&gt;&lt;span class="linenum9" style="DISPLAY: none; BACKGROUND: silver"&gt;   46&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The above code defines the Human class, and each object of the class gets a unique id at the time it is created - which distinguishes it as a unique individual, distinct from others of the same kind (Humans).&lt;br /&gt;&lt;br /&gt;Four humans are created and they are stored in a fixed order in a vector of reference wrapped Humans. This is necessary because we could not have had a vector&amp;lt;Human&amp;gt; since Human is not a copiable class. Since boost::reference_wrapper&amp;lt;T&amp;gt; is copiable, we can create a vector&amp;lt;boost::reference_wrapper&amp;lt;T&amp;gt; &amp;gt;.&lt;br /&gt;&lt;br /&gt;Finally, we print the unique id of each Human in the people vector. If you notice, we actually reference wrapped and stored &lt;i&gt;const Human&lt;/i&gt;, and we created such reference wrapped objects from regular objects using the call &lt;i&gt;boost::cref(...)&lt;/i&gt;. You could have used &lt;i&gt;boost::ref(...)&lt;/i&gt; instead to create non-const references. Most importantly, the functor PrintId does not need to know anything about reference wrappers. It's overloaded operator() takes a normal reference to a const Human, and when the for_each algorithm is used, the reference wrapper elements from the vector gracefully degrade to const Human&amp;amp;. This is huge convenience if you consider how brief the code is.&lt;br /&gt;&lt;br /&gt;But if you thought this is the limit of reference_wrapper's applicability, you have been deceived. The reference_wrapper class is not only an idiom-enabling construct (allowing you to store references in STL containers), it is also an optimization-enabling construct and in many cases, changing existing code to use reference_wrappers can actually reap tremendous performance benefits. Consider the following piece of STL code:&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum10')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum10"&gt;&lt;a href="javascript:hideLineNums('linenum10')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Foo {&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt;     Foo() : id_(++count) {&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;         cout &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Object constructed: id = "&lt;/span&gt; &amp;lt;&amp;lt; id_ &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;     &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; id() &lt;span style="color:blue;"&gt;const&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; id_;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;     Foo(&lt;span style="color:blue;"&gt;const&lt;/span&gt; Foo&amp;amp; srt) : id_(++count) {&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;         cout &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Object constructed: id = "&lt;/span&gt; &amp;lt;&amp;lt; id_&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;              &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;", copied from Object[ id = "&lt;/span&gt; &amp;lt;&amp;lt; srt.id() &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;" ]"&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;     Foo&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt; = (&lt;span style="color:blue;"&gt;const&lt;/span&gt; Foo&amp;amp; srt) {&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; *this;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt; &lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt;     &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; id_;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt;     &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; count;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; Foo::count = 0;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt; &lt;span style="color:blue;"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; T&amp;gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Bar {&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   34&lt;/span&gt;     &lt;span style="color:blue;"&gt;int&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;()(T t) {&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   35&lt;/span&gt;         cout &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Object[ id = "&lt;/span&gt; &amp;lt;&amp;lt; t.id() &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;" ]"&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   36&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   37&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   38&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   39&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   40&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   41&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main() {&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   42&lt;/span&gt;     &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::vector;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   43&lt;/span&gt;     Foo f1, f2, f3, f4;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   44&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   45&lt;/span&gt;     vector&amp;lt; Foo &amp;gt; foos;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   46&lt;/span&gt;     foos.reserve(4);&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   47&lt;/span&gt;     foos.push_back(f1);&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   48&lt;/span&gt;     foos.push_back(f2);&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   49&lt;/span&gt;     foos.push_back(f3);&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   50&lt;/span&gt;     foos.push_back(f4);&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   51&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   52&lt;/span&gt;     std::for_each(foos.begin(), foos.end(), Bar&amp;lt;Foo &lt;span style="color:blue;"&gt;const&lt;/span&gt;&amp;amp;&amp;gt;());&lt;br /&gt;&lt;span class="linenum10" style="DISPLAY: none; BACKGROUND: silver"&gt;   53&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This shows a regular class Foo, which retains a unique id (from the Human example) being stored in a vector&amp;lt; Foo &amp;gt; and then the functor Bar&amp;lt;Foo const&amp;amp;&amp;gt; being invoked on each element of the vector. The Foo object is copiable and assignable, although on copy-construction the new object gets its own unique id, and even on assignment, this unique id is retained and not overwritten. Whenever a new object is created through the default constructor, a message of the form:&lt;br /&gt;&lt;br /&gt;Object constructed: id = &amp;lt;object_id&amp;gt;&lt;br /&gt;&lt;br /&gt;is printed. Whenever a new object is created through the copy constructor, a similar message of the form:&lt;br /&gt;&lt;br /&gt;Object constructed: id = &amp;lt;object_id&amp;gt;, copied from Object[ id = &amp;lt;source_object_id&amp;gt; ]&lt;br /&gt;&lt;br /&gt;is printed. On my system, this prints the following messages.&lt;br /&gt;&lt;pre&gt;Object constructed: id = 1&lt;br /&gt;Object constructed: id = 2&lt;br /&gt;Object constructed: id = 3&lt;br /&gt;Object constructed: id = 4&lt;br /&gt;Object constructed: id = 5, copied from Object[ id = 1 ]&lt;br /&gt;Object constructed: id = 6, copied from Object[ id = 2 ]&lt;br /&gt;Object constructed: id = 7, copied from Object[ id = 3 ]&lt;br /&gt;Object constructed: id = 8, copied from Object[ id = 4 ]&lt;br /&gt;Object[ id = 5 ]&lt;br /&gt;Object[ id = 6 ]&lt;br /&gt;Object[ id = 7 ]&lt;br /&gt;Object[ id = 8 ]&lt;/pre&gt;&lt;br /&gt;If I remove the call to the reserve method of the vector at line 46, you might see a few more copies of the Foo objects being done as the vector initially expands upon each push_back. On my system (MSVC 9), with line 46 commented, the output looks like:&lt;br /&gt;&lt;pre&gt;Object constructed: id = 1&lt;br /&gt;Object constructed: id = 2&lt;br /&gt;Object constructed: id = 3&lt;br /&gt;Object constructed: id = 4&lt;br /&gt;Object constructed: id = 5, copied from Object[ id = 1 ]&lt;br /&gt;Object constructed: id = 6, copied from Object[ id = 5 ]&lt;br /&gt;Object constructed: id = 7, copied from Object[ id = 2 ]&lt;br /&gt;Object constructed: id = 8, copied from Object[ id = 6 ]&lt;br /&gt;Object constructed: id = 9, copied from Object[ id = 7 ]&lt;br /&gt;Object constructed: id = 10, copied from Object[ id = 3 ]&lt;br /&gt;Object constructed: id = 11, copied from Object[ id = 8 ]&lt;br /&gt;Object constructed: id = 12, copied from Object[ id = 9 ]&lt;br /&gt;Object constructed: id = 13, copied from Object[ id = 10 ]&lt;br /&gt;Object constructed: id = 14, copied from Object[ id = 4 ]&lt;br /&gt;Object[ id = 11 ]&lt;br /&gt;Object[ id = 12 ]&lt;br /&gt;Object[ id = 13 ]&lt;br /&gt;Object[ id = 14 ]&lt;/pre&gt;&lt;br /&gt;Things look only marginally better with gcc 4.1. This copying is at its peak initially when the vector starts to grow because the vector's storage is repeatedly reallocated. It becomes much less pronounced when the vector becomes larger. This should be a good enough reason to use the reserve method to optimize on unnecessary reallocation and copying.&lt;br /&gt;&lt;br /&gt;If you now notice, in this piece of code, we are not retaining using the vector after any of the Foo objects f1, f2, f3 or f4 go out of scope. So, if we could store references to the Foo objects in the vector, it would have been a lot more efficient - with almost no copying needed. Here is the code for it. You can compare its output with that of the code above.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum11')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum11"&gt;&lt;a href="javascript:hideLineNums('linenum11')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/ref.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Foo {&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt;     Foo() : id_(++count) {&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt;         cout &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Object constructed: id = "&lt;/span&gt; &amp;lt;&amp;lt; id_ &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt;     &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; id() &lt;span style="color:blue;"&gt;const&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; id_;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;     Foo(&lt;span style="color:blue;"&gt;const&lt;/span&gt; Foo&amp;amp; srt) : id_(++count) {&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;         cout &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Object constructed: id = "&lt;/span&gt; &amp;lt;&amp;lt; id_&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;              &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;", copied from Object[ id = "&lt;/span&gt; &amp;lt;&amp;lt; srt.id() &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;" ]"&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;     Foo&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt; = (&lt;span style="color:blue;"&gt;const&lt;/span&gt; Foo&amp;amp; srt) {&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; *this;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt; &lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt;     &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; id_;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt;     &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; count;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; Foo::count = 0;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt; &lt;span style="color:blue;"&gt;template&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; T&amp;gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   34&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; Bar {&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   35&lt;/span&gt;     &lt;span style="color:blue;"&gt;int&lt;/span&gt; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;()(T t) {&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   36&lt;/span&gt;         cout &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Object[ id = "&lt;/span&gt; &amp;lt;&amp;lt; t.id() &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;" ]"&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   37&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   38&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   39&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   40&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   41&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   42&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main() {&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   43&lt;/span&gt;     &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::vector;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   44&lt;/span&gt;     Foo f1, f2, f3, f4;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   45&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   46&lt;/span&gt;     vector&amp;lt; boost::reference_wrapper&amp;lt;Foo &lt;span style="color:blue;"&gt;const&lt;/span&gt;&amp;gt; &amp;gt; foos;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   47&lt;/span&gt;     foos.reserve(4);&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   48&lt;/span&gt;     foos.push_back(boost::cref(f1));&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   49&lt;/span&gt;     foos.push_back(boost::cref(f2));&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   50&lt;/span&gt;     foos.push_back(boost::cref(f3));&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   51&lt;/span&gt;     foos.push_back(boost::cref(f4));&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   52&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   53&lt;/span&gt;     std::for_each(foos.begin(), foos.end(), Bar&amp;lt;Foo &lt;span style="color:blue;"&gt;const&lt;/span&gt;&amp;amp;&amp;gt;());&lt;br /&gt;&lt;span class="linenum11" style="DISPLAY: none; BACKGROUND: silver"&gt;   54&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You'd see the following output - indicating that absolutely no copies needed to be made.&lt;br /&gt;&lt;pre&gt;Object constructed: id = 1&lt;br /&gt;Object constructed: id = 2&lt;br /&gt;Object constructed: id = 3&lt;br /&gt;Object constructed: id = 4&lt;br /&gt;Object[ id = 1 ]&lt;br /&gt;Object[ id = 2 ]&lt;br /&gt;Object[ id = 3 ]&lt;br /&gt;Object[ id = 4 ]&lt;/pre&gt;&lt;br /&gt;This is an important optimization - the kind there is a lot of scope for, and so you should always look out in your code for an opportunity to do such optimizations, using boost::ref and boost::cref.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;5. Expressive use of STL algorithms with lambda functions&lt;/h4&gt;&lt;br /&gt;This short section is the least trivial of all the "week 1" techniques we are going to discuss here, because here I will whet your appetite for the enormous amounts of functional programming that Boost supports. We will take a look at the Lambda library - a library for creating unnamed functions, based on &lt;a href="http://www.blogger.com/2008/07/expression-templates-demystified.html"&gt;expression templates&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The beauty and power of the Lambda library lies in the fact that one can create specialized functions (or rather &lt;i&gt;functors&lt;/i&gt; or &lt;i&gt;function objects&lt;/i&gt;) on the fly and invoke them by various means. These functions don't need a name and have an implicit prototype that can easily change.&lt;br /&gt;&lt;br /&gt;In the example in the section 2 (on string algorithms) above, we extracted the AccountSummary data for each individual from the delimited text stream. The data is now present in the form of a vector. What if we want to write this data in a suitable format to the standard output. Say, we want to output each record in a block, like:&lt;br /&gt;&lt;pre&gt;Chaim Rabinowitz&lt;br /&gt;-----------------&lt;br /&gt;age: 69&lt;br /&gt;bank: Citibank N.A.&lt;br /&gt;branch: Tel-Aviv&lt;br /&gt;account_number: 5629311372&lt;br /&gt;balance: 100000&lt;br /&gt;-----------------&lt;br /&gt;&lt;br /&gt;Mordechai Silberman&lt;br /&gt;-----------------&lt;br /&gt;age: 43&lt;br /&gt;bank: Carmel Bank&lt;br /&gt;branch: Haifa&lt;br /&gt;account_number: 46268675539&lt;br /&gt;balance: 20000&lt;br /&gt;-----------------&lt;br /&gt;...&lt;/pre&gt;&lt;br /&gt;We could write a function object and pass it to the std::for_each algorithm as the applicative functor to do the job for us - for_each iterates over each element in the vector and prints it in the format supported by the functor. But following this, you discover that you also need to put this data in a different delimited format like:&lt;br /&gt;&lt;pre&gt;Chaim Rabinowitz|age: 69|bank: Citibank N.A.|branch: Tel-Aviv|account_number: 5629311372|balance: 100000&lt;br /&gt;Mordechai Silberman|age: 43|bank: Carmel Bank|branch: Haifa|account_number: 46268675539|balance: 20000&lt;/pre&gt;&lt;br /&gt;What do you do - fire up your C++ editor and add another piece of functor. The number of formats you want your data to be printed in is really open-ended - may be you just want to summarize the name and age of the individuals - yet another functor? You must be thinking by now that you want an easier way to do this. After all you might be defining all these functors in some other source or header file, while you might be using them somewhere completely removed from the place they are defined in. Your operative call would probably just look like:&lt;pre&gt;for_each(account_data.begin(), account_data.end(), PrintBlocks());&lt;br /&gt;for_each(account_data.begin(), account_data.end(), PrintPipeDelimited());&lt;br /&gt;for_each(account_data.begin(), account_data.end(), PrintSummary());&lt;/pre&gt;&lt;br /&gt;There is nothing wrong with such code provided we have reasonable names for the functor classes like PrintBlocks, PrintPipeDelimited or PrintSummary (all of which are assumed to have a trivial default constructor here). But can you look beyond the PrintBlocks name and figure out the format in which the data is actually printed by it - or figure out what constitutes a summary in PrintSummary. You cannot - and you don't want to depend on an aspect of programming that depends on appropriateness of naming identifiers. Besides, such formatting detail is not worth encapsulating in functors and storing away - how much reusable are these functors really going to be. This is where Boost Lambda helps you in being expressive. Below is the slightly curious looking rewrite of the above code using Boost Lambda, and with all the formatting logic spelled out &lt;i&gt;in-place&lt;/i&gt;. The code essentially extends whats's already there, so only the newer parts of the code are provided, with only a bit of the old code for context. Please don't be exasperated by syntactic unfamiliarity - try to get the big picture of what's being done.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum8')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum8"&gt;&lt;a href="javascript:hideLineNums('linenum8')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  1-4&lt;/span&gt;&lt;span style="color:teal;"&gt;//...&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/lambda/lambda.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/lambda/bind.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt; 8-16&lt;/span&gt;&lt;span style="color:teal;"&gt;//...&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::for_each;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; boost::lambda::bind;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; boost::lambda::constant;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; boost::lambda::_1;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; AccountSummary {&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;     string name;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;     &lt;span style="color:blue;"&gt;int&lt;/span&gt; age;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt;     string bank;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt;     string branch;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt;     string account_number;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt;     &lt;span style="color:blue;"&gt;double&lt;/span&gt; balance;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;     AccountSummary() : age(0), balance(0.0) {&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt; };&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   34&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main()&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   35&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   36&lt;/span&gt;     string str = &lt;span style="color:red;"&gt;"{name:Chaim Rabinowitz;acc:5629311372;bank:Citibank N.A.;branch:Tel-Aviv;age:69;balance:100000.00}"&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   37&lt;/span&gt;                  &lt;span style="color:red;"&gt;"{name:Mordechai Silberman;acc:46268675539;bank:Carmel Bank;branch:Haifa;age:43;balance:20000.00}"&lt;/span&gt;;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   38&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;39-44&lt;/span&gt;&lt;span style="color:teal;"&gt;//...&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   45&lt;/span&gt;     &lt;span style="color:blue;"&gt;for&lt;/span&gt; (tokenizer::iterator tok_iter = records.begin(); tok_iter != records.end(); ++tok_iter) {&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;46-85&lt;/span&gt;&lt;span style="color:teal;"&gt;//...&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   85&lt;/span&gt;         account_data.push_back(next_acc_data);&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   86&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   87&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   88&lt;/span&gt;     &lt;span style="color:blue;"&gt;typedef&lt;/span&gt; vector&amp;lt;AccountSummary&amp;gt;::value_type elem_type;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   89&lt;/span&gt;     for_each(account_data.begin(), account_data.end(),&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   90&lt;/span&gt;                cout &amp;lt;&amp;lt; '\n' &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Block:"&lt;/span&gt; &amp;lt;&amp;lt; '\n'&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   91&lt;/span&gt;                     &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"--------------------"&lt;/span&gt; &amp;lt;&amp;lt; '\n'&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   92&lt;/span&gt;                     &amp;lt;&amp;lt; bind(&amp;amp;elem_type::name, _1) &amp;lt;&amp;lt; constant('\n')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   93&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"-----------------"&lt;/span&gt;) &amp;lt;&amp;lt; constant('\n')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   94&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"age: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::age, _1) &amp;lt;&amp;lt; constant('\n')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   95&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"bank: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::bank, _1) &amp;lt;&amp;lt; constant('\n')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   96&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"branch: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::branch, _1) &amp;lt;&amp;lt; constant('\n')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   97&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"account_number: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::account_number, _1) &amp;lt;&amp;lt; constant('\n')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   98&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"balance: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::balance, _1) &amp;lt;&amp;lt; constant('\n')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;   99&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"-----------------"&lt;/span&gt;) &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"\n\n"&lt;/span&gt;)&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  100&lt;/span&gt;            );&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  101&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  102&lt;/span&gt;     for_each(account_data.begin(), account_data.end(),&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  103&lt;/span&gt;                cout &amp;lt;&amp;lt; '\n' &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Delimited:"&lt;/span&gt; &amp;lt;&amp;lt; '\n'&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  104&lt;/span&gt;                     &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"--------------------"&lt;/span&gt; &amp;lt;&amp;lt; '\n'&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  105&lt;/span&gt;                     &amp;lt;&amp;lt; bind(&amp;amp;elem_type::name, _1) &amp;lt;&amp;lt; constant('')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  106&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"age: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::age, _1) &amp;lt;&amp;lt; constant('')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  107&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"bank: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::bank, _1) &amp;lt;&amp;lt; constant('')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  108&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"branch: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::branch, _1) &amp;lt;&amp;lt; constant('')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  109&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"account_number: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::account_number, _1) &amp;lt;&amp;lt; constant('')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  110&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"balance: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::balance, _1) &amp;lt;&amp;lt; constant('\n')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  111&lt;/span&gt;            );&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  112&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  113&lt;/span&gt;     for_each(account_data.begin(), account_data.end(),&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  114&lt;/span&gt;                cout &amp;lt;&amp;lt; '\n' &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Summary Information:"&lt;/span&gt; &amp;lt;&amp;lt; "\n\n"&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  115&lt;/span&gt;                     &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"--------------------"&lt;/span&gt; &amp;lt;&amp;lt; '\n'&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  116&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"name: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::name, _1) &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;", "&lt;/span&gt;)&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  117&lt;/span&gt;                     &amp;lt;&amp;lt; constant(&lt;span style="color:red;"&gt;"age: "&lt;/span&gt;) &amp;lt;&amp;lt; bind(&amp;amp;elem_type::age, _1) &amp;lt;&amp;lt; constant('\n')&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  118&lt;/span&gt;            );&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  119&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  120&lt;/span&gt;     &lt;span style="color:blue;"&gt;return&lt;/span&gt; EXIT_SUCCESS;&lt;br /&gt;&lt;span class="linenum8" style="DISPLAY: none; BACKGROUND: silver"&gt;  121&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The output of this code is of the following form:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Block:&lt;br /&gt;--------------------&lt;br /&gt;Chaim Rabinowitz&lt;br /&gt;-----------------&lt;br /&gt;age: 69&lt;br /&gt;bank: Citibank N.A.&lt;br /&gt;branch: Tel-Aviv&lt;br /&gt;account_number: 5629311372&lt;br /&gt;balance: 100000&lt;br /&gt;-----------------&lt;br /&gt;&lt;br /&gt;Mordechai Silberman&lt;br /&gt;-----------------&lt;br /&gt;age: 43&lt;br /&gt;bank: Carmel Bank&lt;br /&gt;branch: Haifa&lt;br /&gt;account_number: 46268675539&lt;br /&gt;balance: 20000&lt;br /&gt;-----------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Delimited:&lt;br /&gt;--------------------&lt;br /&gt;Chaim Rabinowitzage: 69bank: Citibank N.A.branch: Tel-Avivaccount_number: 5629311372balance: 100000&lt;br /&gt;Mordechai Silbermanage: 43bank: Carmel Bankbranch: Haifaaccount_number: 46268675539balance: 20000&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Summary Information:&lt;br /&gt;--------------------&lt;br /&gt;name: Chaim Rabinowitz, age: 69&lt;br /&gt;name: Mordechai Silberman, age: 43&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If you see, we have printed the data in the AccountSummary records present in the vector in three different formats - Blocked, Delimited and Summary information. We didn't use a for-loop but rather the &lt;i&gt;for_each&lt;/i&gt; algorithm to iterate through the records in each vector. The for_each algorithm takes three arguments - the two iterators that define the range of records to be handled, and a function pointer or functor which takes a single element of the container (in this case an AccountSummary object) and does some processing on it. The for_each statements for the three different cases (Blocked, Delimited and Summary) appear at lines 89, 102 and 113 respectively. In each case, the first two arguments are the iterators marking the beginning and past-the-end location of the AccountSummary vector. But for the third argument - we have a &lt;i&gt;Lambda expression&lt;/i&gt; or an &lt;i&gt;unnamed function&lt;/i&gt; that &lt;i&gt;evaluates to a function object&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;A Lambda expression is really just an expression, whose value is a function object. Just as 5+3 is an expression which results in an integer object, the Lambda expression is an expression which generates a callable entity - a functor object. The type of this functor object is a derived type which is of no consequence here. The Lambda expression is really a way of composing smaller function objects into a more complex function object, using overloaded operators to string together the smaller function objects. Thus expressions such as bind(...), constant(...) and _1 are all function objects with overloaded operators between them. Indeed, a Lambda expression is an excellent example of &lt;i&gt;functional composition&lt;/i&gt; - the core idea in &lt;i&gt;functional programming&lt;/i&gt;. More explanation follows.&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;In each of these Lambda expressions, the curious looking _1 represents a "placeholder" for the first argument to the function. Because a Lambda expression evaluates to a functor of some sort and this functor has a signature of its own - it is but natural that we need a way to reference the arguments to this functor inside the Lambda expression. Boost's Lambda expressions support upto 9 arguments - by default represented by the placeholders _1 through _9. They are just normal C++ objects (of some type, not important here) and intelligently named using the fact that any valid C identifier can start with an underscore. In this case, there is only one argument to deal with - the current element in the vector - so we only need _1. But we need to get at its members.&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;An expression like bind(&amp;amp;elem_type::name, _1) creates a functor which returns the value of the data member &lt;i&gt;name&lt;/i&gt; of the object represented by _1, whose type is elem_type. Here elem_type is typedef'd to mean the value_type of the vector (i.e. AccountSummary). For example, if you were to use for_each to iterate over a map of string keys and double values, then at each step you'd be getting elements whose type is std::map&amp;lt;string, double&amp;gt;::value_type. This is essentially an std::pair&amp;lt;string, double&amp;gt; with members &lt;i&gt;first&lt;/i&gt; (string) and &lt;i&gt;second&lt;/i&gt; (double). Then, in order to construct a functor which returns the string key, we would use a bind expression like this:&lt;br /&gt;&lt;br /&gt;bind(std::map&amp;lt;string, double&amp;gt;::value_type::first, _1);&lt;br /&gt;&lt;br /&gt;We can almost always abridge these expressions by using suitable typedefs. For example:&lt;br /&gt;&lt;br /&gt;typedef std::map&amp;lt;string, double&amp;gt;::value_type map_elem;&lt;br /&gt;bind(map_elem::first, _1);&lt;br /&gt;&lt;br /&gt;Typically, we should try to use a single typedef'd name, corresponding to the type of _1 (or whichever argument placeholder).&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Expressions like constant("name: ") or constant('\n') represent functors which return a constant value. We don't want literals in the lambda expression - rather function objects which return such literals. That's because they need to be invoked for each iteration - not just printed once. However, you would have noticed that "Delimited:" or "Block:" are not enclosed in constant(...). In the output, you should see that &lt;i&gt;name:&lt;/i&gt; or &lt;i&gt;age:&lt;/i&gt; are repeated for each record - these were enclosed in constant(...) - but "Delimited:" or "Block:" were printed only once - because these were not enclosed in constant(...).&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;In the same way as &lt;i&gt;constant&lt;/i&gt;, there is another construct - &lt;i&gt;var&lt;/i&gt;, which can wrap non-literal object instances. We haven't used it here but we could.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;When all of these are combined into a single Lambda expression, it represents a callable entity - essentially a generated super-function, built from the above individual functional units. By now, you should have a gut feel of how Lambda expressions work - but I will leave you with another example to squint your eyes at.&lt;br /&gt;&lt;br /&gt;The real power of Lambda expressions is the ability to define a callable entity at the site of invocation in a fairly intuitive and succinct manner. There is a syntactic baggage associated - and I feel that's the only factor that weighs against Lambda expressions. But it requires a bit of mastering, and you'd be on your way to leveraging the full power of Lambda expressions.&lt;br /&gt;&lt;br /&gt;In order to get a hang of how functional composition works, read the two part article &lt;a href="http://shoddykid.blogspot.com/2008/07/expression-templates-demystified.html"&gt;Expression Templates demystified&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;6. Platform-independent code to manipulate files on the disk&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;7. Quick and easy threads&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;8. An extensible way to handle command-line arguments&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;9. Matching patterns and replacing text using Regular Expressions&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;Regular expressions are an important programming tool and is extensively used in applications for filtering and parsing text, and matching names of objects against name patterns. Every language used for application programming owes itself a regular expression facility, and with the slick and fast Boost.Regex, C++ gets its first standard Regular Expression library.&lt;br /&gt;&lt;br /&gt;This section cannot serve as an introduction to the theory and syntax of &lt;a href="http://en.wikipedia.org/wiki/Regular_expression"&gt;regular expressions&lt;/a&gt;. Assuming that you have a working knowledge of Regular expression syntax as used by grep and egrep on Unix, or the Perl language, we get started here with a few short examples of how to use the Boost.Regex library.&lt;br /&gt;&lt;br /&gt;To use the Boost.Regex library, you should know its basic object model - the main classes whose objects you need to define and use for your Regex work.&lt;br /&gt;&lt;ol&gt;&lt;li&gt;A regular expression is encapsulated by an instance of the class &lt;i&gt;boost::regex&lt;/i&gt;. In fact, boost::regex is a typedef for &lt;i&gt;boost::basic_regex&amp;lt;char&amp;gt;&lt;/i&gt;. There is a wide character version available - &lt;i&gt;boost::wregex&lt;/i&gt;, which is a typedef for &lt;i&gt;boost::basic_regex&amp;lt;wchar_t&amp;gt;&lt;/i&gt;.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The result of trying to search for a regular expression in an input can result in matches at several levels - including matches for sub-expressions within regular expressions. All such details of matches are reported back in an object of type &lt;i&gt;match_results&lt;/i&gt;. Once again, match_results is a class template, and there are concrete instantiations like &lt;i&gt;cmatch&lt;/i&gt;, &lt;i&gt;smatch&lt;/i&gt; and their wide character variants (&lt;i&gt;wcmatch&lt;/i&gt; and &lt;i&gt;wsmatch&lt;/i&gt;) which correspond to specializations for C-strings (char*), &lt;i&gt;C++ strings&lt;/i&gt; and wchar_t* strings.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The iterator &lt;i&gt;regex_iterator&lt;/i&gt; which helps you run through the input sequence identifying all matches for a particular regular expression. This is a really handy tool.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Finally, the &lt;i&gt;regex_token_iterator&lt;/i&gt; iterator adaptor - which makes splitting input based on delimiters (like &lt;i&gt;split&lt;/i&gt; in Perl) as easy as it gets in C++.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;In our first example of this section, we will write a simple egrep like utility which looks for regular expression patterns in a text stream, and prints out lines matching the pattern. It also prints the line number, and the segment of the line that matched the regular expression (in brackets). The program is invoked with a regular expression and one or more input file names as command-line arguments.&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none"&gt;&lt;a href="javascript:hideLineNums('linenum12')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum12"&gt;&lt;a href="javascript:hideLineNums('linenum12')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;    1&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;boost/regex.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;    2&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;    3&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;fstream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;    4&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;    5&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;    6&lt;/span&gt; &lt;span style="color:green;"&gt;#include&lt;/span&gt; &lt;span style="color:teal;"&gt;&amp;lt;exception&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;    7&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;    8&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cout;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;    9&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::cerr;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   10&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::endl;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   11&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::ifstream;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   12&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::string;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   13&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; std::exception;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   14&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   15&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; main(&lt;span style="color:blue;"&gt;int&lt;/span&gt; argc, &lt;span style="color:blue;"&gt;char&lt;/span&gt; *argv[])&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   16&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   17&lt;/span&gt;     &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( argc &amp;lt;= 2 ) {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   18&lt;/span&gt;         cerr &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Usage: "&lt;/span&gt; &amp;lt;&amp;lt; argv[0] &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;" &amp;lt;regex&amp;gt; &amp;lt;file-list&amp;gt;"&lt;/span&gt;&amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   19&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; EXIT_FAILURE;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   20&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   21&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   22&lt;/span&gt;     string str;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   23&lt;/span&gt;     ifstream in;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   24&lt;/span&gt;     &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;char&lt;/span&gt; * file_name = 0;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   25&lt;/span&gt; &lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   26&lt;/span&gt;     &lt;span style="color:blue;"&gt;try&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   27&lt;/span&gt;         boost::regex reg(argv[1]);&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   28&lt;/span&gt;         &lt;span style="color:blue;"&gt;while&lt;/span&gt; ( ( file_name = argv[--argc] )&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   29&lt;/span&gt;                 &amp;amp;&amp;amp; argc &amp;gt; 1 )&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   30&lt;/span&gt;         {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   31&lt;/span&gt;             in.open(file_name, std::ios::in|std::ios::beg);&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   32&lt;/span&gt;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   33&lt;/span&gt;             &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( in ) {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   34&lt;/span&gt;                 cout &amp;lt;&amp;lt; file_name &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"==================="&lt;/span&gt;&amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   35&lt;/span&gt;                 boost::smatch m;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   36&lt;/span&gt;                 &lt;span style="color:blue;"&gt;int&lt;/span&gt; line_no = 0;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   37&lt;/span&gt;                 &lt;span style="color:blue;"&gt;while&lt;/span&gt;( getline(in, str) ) {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   38&lt;/span&gt;                     ++line_no;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   39&lt;/span&gt;                     &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( boost::regex_search(str, m, reg) ) {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   40&lt;/span&gt;                         cout &amp;lt;&amp;lt; line_no &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;": "&lt;/span&gt; &amp;lt;&amp;lt; str &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"  [matches: "&lt;/span&gt; &amp;lt;&amp;lt; string(m[0].first, m[0].second) &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"]"&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   41&lt;/span&gt;                     }&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   42&lt;/span&gt;                 }&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   43&lt;/span&gt;             } &lt;span style="color:blue;"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   44&lt;/span&gt;                 cerr &amp;lt;&amp;lt; file_name &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;": Could not read."&lt;/span&gt; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"==================="&lt;/span&gt;&amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   45&lt;/span&gt;                 &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( !in ) in.clear();&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   46&lt;/span&gt;             }&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   47&lt;/span&gt;         }&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   48&lt;/span&gt;     } &lt;span style="color:blue;"&gt;catch&lt;/span&gt;(exception&amp;amp; e) {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   49&lt;/span&gt;         cerr &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Exception caught: "&lt;/span&gt; &amp;lt;&amp;lt; e.what() &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   50&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; EXIT_FAILURE;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   51&lt;/span&gt;     } &lt;span style="color:blue;"&gt;catch&lt;/span&gt;(...) {&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   52&lt;/span&gt;         cerr &amp;lt;&amp;lt; &lt;span style="color:red;"&gt;"Exception caught."&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   53&lt;/span&gt;         &lt;span style="color:blue;"&gt;return&lt;/span&gt; EXIT_FAILURE;&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   54&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum12" style="DISPLAY: none; BACKGROUND: silver"&gt;   55&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If you notice, the above example has a small issue. It picks up each line, searches for a match for the regular expression, and as soon as the first match is found, it prints the line number, line and the matching segment (in brackets). It then moves to the next line. If there are multiple segments in a single line matching the given regular expression, we miss all of those segments except the first one.&lt;br /&gt;&lt;br /&gt;While it is possible to use the same regex_search function in a loop and using string iterators to run through all matches per line of text, the code doesn't look very good. In case you have to run through all distinct segments within an input sequence that match a regular expression, use the &lt;i&gt;regex_iterator&lt;/i&gt;. A regex_iterator "points to" a &lt;i&gt;match_result&lt;/i&gt; object. A match_result object represents a segment of input that matches a regular expression (as well as sub-expression matches, etc.). Thus, using regex_iterator, we loop over all matches in an input sequence. Here is the slightly altered code ... only the changed lines are highlighted.&lt;br /&gt;&lt;br /&gt;&lt;span class="linenum13" style="display:none"&gt;&lt;a href="javascript:hideLineNums('linenum13')"&gt;Hide line numbers&lt;/a&gt;&lt;/span&gt;&lt;span class="linenum13"&gt;&lt;a href="javascript:hideLineNums('linenum13')"&gt;Show line numbers&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   37&lt;/span&gt; &lt;span style="color:blue"&gt;while&lt;/span&gt;( getline(in, str) ) {&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   38&lt;/span&gt;     ++line_no;&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   39&lt;/span&gt;     boost::sregex_iterator it(str.begin(), str.end(), reg), end;&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   40&lt;/span&gt;     string matchlist;&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   41&lt;/span&gt;     &lt;span style="color:blue"&gt;while&lt;/span&gt; ( it != end ) {&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   42&lt;/span&gt;         matchlist += (*it++)[0].str() + &lt;span style="color:red"&gt;" "&lt;/span&gt;;&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   43&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   44&lt;/span&gt;     &lt;span style="color:blue"&gt;if&lt;/span&gt; ( matchlist.length() ) {&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   45&lt;/span&gt;         cout &amp;lt;&amp;lt; line_no &amp;lt;&amp;lt; &lt;span style="color:red"&gt;": "&lt;/span&gt; &amp;lt;&amp;lt; str &amp;lt;&amp;lt; &lt;span style="color:red"&gt;"  [matches: "&lt;/span&gt; &amp;lt;&amp;lt; matchlist &amp;lt;&amp;lt; &lt;span style="color:red"&gt;"]"&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   46&lt;/span&gt;     }&lt;br /&gt;&lt;span class="linenum13" style="display:none;background:silver"&gt;   47&lt;/span&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;There are just three things to note in the above example. On line 39, we instantiate two objects of sregex_iterator - this is a typedef for regex_iterator&lt;string&gt;. The second of these objects (&lt;i&gt;end&lt;/i&gt;)is default constructed and serves as an end-marker or null-marker. Finally, on line 42, (*it) is an smatch (match_result&lt;string&gt;) object and (*it)[0] represents the part of input that matched the whole Regular Expression. If there are N sub-expressions within the regular expression, (*it)[1] through (*it)[N] represent parts matching those sub-expressions. The str() member function gives the matching part as a C++ string.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;10. Easy date and time measurements&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- &lt;h4&gt;11. Using variant types&lt;/h4&gt; --&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Having come thus far, I must clarify that these ten libraries are in no way the ten most important libraries to know in Boost. There are many others which find no mention in this article - Boost.Interprocess, Boost.Asio, Boost.IoStream, Boost.Graph and the seminal Boost.MPL (The Boost Template Metaprogramming Library) among many others. The reason for not having covered these is that these are more involved libraries and we need to devote more space and time to each one to leverage their power. Therefore, they don't belong to a '10 nifty tricks' list. I'll be back soon with some of these topics.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-6269474965255702309?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/6269474965255702309/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=6269474965255702309' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/6269474965255702309'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/6269474965255702309'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2008/07/getting-started-with-boost_12.html' title='Getting Started with Boost'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-7338366797684674112</id><published>2008-07-12T15:02:00.014+05:30</published><updated>2008-07-13T15:14:41.468+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='smart pointers'/><category scheme='http://www.blogger.com/atom/ns#' term='templates'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++ Challenge of the Month: July 2008</title><content type='html'>&lt;p&gt;Starting this month, I would be posting a C++ programming problem, and it's solution every month in a series called C++ Challenge of the Month (CCOM). The solution will be posted a couple of weeks after the problem and in the meanwhile, I hope to see a few readers try their hand at the problem. So here we go.&lt;/p&gt;&lt;br /&gt;&lt;div style="BACKGROUND: silver; border: thin solid black; padding: 1em"&gt;&lt;h4&gt;CCOM #1: Write the outline of a smart pointer class capable of managing pointers to classes from hierarchies which don't have a virtual destructor&lt;br /&gt;&lt;/h4&gt;&lt;p&gt;Need a smart pointer, which can be used like:&lt;br /&gt;&lt;br /&gt;&lt;font face="courier"&gt;smart_ptr&amp;lt;base&amp;gt; p = new derived;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;When p goes out of scope, the object must be deleted and the destructor of derived must be called, irrespective of whether ~base() is virtual or not.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;Courtesy, Syam Krishnan, Thiruvananthapuram, Kerala.&lt;/i&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-7338366797684674112?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/7338366797684674112/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=7338366797684674112' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/7338366797684674112'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/7338366797684674112'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2008/07/c-challenge-of-month-july-2008.html' title='C++ Challenge of the Month: July 2008'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-2489190527883166864</id><published>2008-07-09T01:12:00.009+05:30</published><updated>2009-09-19T14:24:18.010+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='list of C++ books'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='C++ books'/><title type='text'>Book mania</title><content type='html'>This list includes almost all books that have been published on C++ and that are of any relevance today. More importantly, I have tried to exclude books which you really don't need to read at all - for example Robert Lafore's introductory book on C++. These may not be bad books - just that there are better, more readable books already available.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;List 1:&lt;/strong&gt; For the beginner in programming, choose any one of the following. Even if you have some experience in other programming languages, the following books will provide you a robust foundation in C++.&lt;br /&gt;&lt;br /&gt;&lt;div style="background: silver; border: thin solid black; padding: 1em"&gt;&lt;br /&gt;1. &lt;strong&gt;Programming: Principles and Practice using C++&lt;/strong&gt; - Bjarne Strostrup&lt;br /&gt;2. &lt;strong&gt;C++ Primer&lt;/strong&gt; - Stan Lippman, Joseé Lajoie&lt;br /&gt;3. &lt;strong&gt;Thinking in C++ (vol 1 &amp;amp; 2)&lt;/strong&gt; - Bruce Eckel&lt;br /&gt;4. &lt;strong&gt;Big C++&lt;/strong&gt; - Cay Horstmann, Timothy Budd&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;List 2:&lt;/strong&gt; If you have a fair bit of programming experience but need a quick introduction to C++, read through the following texts one by one, work through their exercises and grow from strength to strength.&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;div style="background: silver; border: thin solid black; padding: 1em"&gt;&lt;br /&gt;1. &lt;strong&gt;Accelerated C++&lt;/strong&gt; - Andrew König, Barbara Moo&lt;br /&gt;2. &lt;strong&gt;Effective C++ 3/e&lt;/strong&gt; - Scott Meyers&lt;br /&gt;3. &lt;strong&gt;The C++ Programming Language&lt;/strong&gt; - Bjarne Stroustrup&lt;br /&gt;4. &lt;strong&gt;The STL Tutorial and Reference Guide&lt;/strong&gt; - Dave Musser, Atul Saini (the ANSI standard edition)&lt;br /&gt;5. &lt;strong&gt;C++ Templates&lt;/strong&gt; - Nicolai M Josuttis, David Vandevoorde&lt;br /&gt;6. &lt;strong&gt;Essential C++&lt;/strong&gt; - Stan Lippman&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;List 3:&lt;/strong&gt; For the practising C++ programmer, these are the books you should be referring to every now and then as you write serious C++ code. In fact, you would often need to refer to some books from List 2 above.&lt;br /&gt;&lt;br /&gt;&lt;div style="background: silver; border: thin solid black; padding: 1em"&gt;&lt;br /&gt; 1. &lt;strong&gt;The C++ Standard Library: Tutorial and Reference Guide&lt;/strong&gt; - Nicolai M Josuttis&lt;br /&gt; 2. &lt;strong&gt;More Effective C++&lt;/strong&gt; - Scott Meyers&lt;br /&gt; 3. &lt;strong&gt;Effective STL&lt;/strong&gt; - Scott Meyers&lt;br /&gt; 4. &lt;strong&gt;More C++ Gems&lt;/strong&gt; - ed Robert Martin&lt;br /&gt; 5. &lt;strong&gt;C++ Cookbook&lt;/strong&gt; - D Ryan Stephens, Christopher Diggins, Jonathan Turkanis, Jeff Cogswell&lt;br /&gt; 6. &lt;strong&gt;Generic Programming with STL: ...&lt;/strong&gt; - Matt Austern&lt;br /&gt; 7. &lt;strong&gt;Exceptional C++&lt;/strong&gt; - Herb Sutter&lt;br /&gt; 8. &lt;strong&gt;The C++ Standard Library Extensions: A tutorial and reference&lt;/strong&gt; - Pete Becker&lt;br /&gt; 9. &lt;strong&gt;More Exceptional C++&lt;/strong&gt; - Herb Sutter&lt;br /&gt;10. &lt;strong&gt;C++ Common Knowledge&lt;/strong&gt; - Steve Dewhurst&lt;br /&gt;11. &lt;strong&gt;Beyond the C++ Standard Library: An Introduction to Boost&lt;/strong&gt; - Björn Karlsson&lt;br /&gt;12. &lt;strong&gt;C++ Coding Standards: 101 guidelines ...&lt;/strong&gt; - Herb Sutter, Andrei Alexandrescu&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;List 4:&lt;/strong&gt; For the wannabe OO-guru, library writer and C++ evangelist:&lt;br /&gt;&lt;br /&gt;&lt;div style="background: silver; border: thin solid black; padding: 1em"&gt;&lt;br /&gt;1. &lt;strong&gt;Design Patterns: Elements of Reusable object Oriented Software&lt;/strong&gt; - Erich Gamma, Richard Helm, John Vlissides, Ralph Hodgson&lt;br /&gt;2. &lt;strong&gt;Modern C++ Design&lt;/strong&gt; - Andrei Alexandrescu&lt;br /&gt;3. &lt;strong&gt;Exceptional C++ Style&lt;/strong&gt; - Herb Sutter&lt;br /&gt;4. &lt;strong&gt;Template Metaprogramming: ...&lt;/strong&gt; - Dave Abrahams, Aleksey Gurtovoy&lt;br /&gt;5. &lt;strong&gt;Imperfect C++&lt;/strong&gt; - Matt Wilson&lt;br /&gt;6. &lt;strong&gt;Pattern Hatching&lt;/strong&gt; - John Vlissides&lt;br /&gt;7. &lt;strong&gt;Extended STL (Vol 1 &amp; 2)&lt;/strong&gt; - Matt Wilson&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;List 5:&lt;/strong&gt;For the language and standard junkie:&lt;br /&gt;&lt;br /&gt;&lt;div style="background: silver; border: thin solid black; padding: 1em"&gt;&lt;br /&gt;1. &lt;strong&gt;The Design and Evolution of C++&lt;/strong&gt; - Bjarne Stroustrup&lt;br /&gt;2. &lt;strong&gt;Inside the C++ Object Model&lt;/strong&gt; - Stan Lippman&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;List 6:&lt;/strong&gt; Books on specific libraries outside C++ and Boost have not been mentioned. A separate list can be created for those. Advanced books on specific topics:&lt;br /&gt;&lt;br /&gt;&lt;div style="background: silver; border: thin solid black; padding: 1em"&gt;&lt;br /&gt;1. &lt;strong&gt;Boost Graph Library&lt;/strong&gt; - Jeremy Siek, Lie-Quan Lee, Andrew Lumsdaine&lt;br /&gt;2. &lt;strong&gt;Standard C++ IoStreams and Locales: ...&lt;/strong&gt; - Angelika Langer, Klaus Kreft&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;List 7:&lt;/strong&gt; Older books still worth a read.&lt;br /&gt;&lt;br /&gt;&lt;div style="background: silver; border: thin solid black; padding: 1em"&gt;&lt;br /&gt;1. &lt;strong&gt;C++ Gems&lt;/strong&gt; - ed Stan Lippman(especially the part on Templates - includes original articles from Todd Veldhuizen)&lt;br /&gt;2. &lt;strong&gt;The Annotated C++ Reference Manual&lt;/strong&gt; - Stroustrup, Margaret Ellis&lt;br /&gt;3. &lt;strong&gt;C++ Gotchas&lt;/strong&gt; - Stephen Dewhurst&lt;br /&gt;4. &lt;strong&gt;C++ Programming Style&lt;/strong&gt; - Tom Cargill&lt;br /&gt;5. &lt;strong&gt;Advanced C++ Programming Styles and Idioms&lt;/strong&gt; - James Coplien&lt;br /&gt;6. &lt;strong&gt;Large Scale C++ Software Design&lt;/strong&gt; - John Lakos&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;List 8:&lt;/strong&gt; A few books that don't focus on C++ at all but general programming and design, and perhaps use other high-level programming languages. But these are very good read for every serious C++ programmer.&lt;br /&gt;&lt;br /&gt;&lt;div style="background: silver; border: thin solid black; padding: 1em"&gt;&lt;br /&gt;1. &lt;strong&gt;Emergent Design&lt;/strong&gt; - Scott L. Bain&lt;br /&gt;2. &lt;strong&gt;Working Effectively with Legacy Code&lt;/strong&gt; - Michael Feathers&lt;br /&gt;3. &lt;strong&gt;Refactoring&lt;/strong&gt; - Martin Fowler, Kent Beck, John Brant&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-2489190527883166864?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/2489190527883166864/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=2489190527883166864' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/2489190527883166864'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/2489190527883166864'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2008/07/book-mania.html' title='Book mania'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-5632940848553059842</id><published>2008-07-04T04:39:00.070+05:30</published><updated>2011-01-04T22:25:39.303+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Boost libraries'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Getting Started with Boost</title><content type='html'>&lt;span class="fullpost"&gt;&lt;h2&gt;Getting Started with Boost&lt;/h2&gt;&lt;br /&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;td colspan="3"&gt;&lt;span style="font-size:85%;"&gt;&lt;b&gt;Table of Contents&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tfoot&gt;&lt;tr&gt;&lt;td colspan="3"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tfoot&gt;&lt;tbody align="left"&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;1.&lt;/span&gt;&lt;/td&gt;&lt;td colspan="2"&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#intro"&gt;Introduction&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;2.&lt;/span&gt;&lt;/td&gt;&lt;td colspan="2"&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#purp"&gt;Purpose &amp;amp; Motivation&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;3.&lt;/span&gt;&lt;/td&gt;&lt;td colspan="2"&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#bboost"&gt;Building Boost&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;3.1&lt;/span&gt;&lt;/td&gt;&lt;br /&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#bboostproc"&gt;The Boost Build Process&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/td&gt;&lt;br /&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;3.2&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#bboostwin"&gt;Building Boost on Windows&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;3.3&lt;/span&gt;&lt;/td&gt;&lt;br /&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#bboostlin"&gt;Building Boost on Linux&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/td&gt;&lt;br /&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;3.4&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#bboostbin"&gt;Boost Binaries&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;4.&lt;/span&gt;&lt;/td&gt;&lt;td colspan="2"&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#boostprog"&gt;Using the Boost Libraries&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;4.1&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#winlink"&gt;Linking to Boost libraries on Windows &lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;4.2&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html#nixlink"&gt;Linking to Boost libraries on Unix-like system &lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;1. &lt;a href="javascript:void(0);" name="intro"&gt;Introduction&lt;/a&gt;&lt;/h3&gt;&lt;br /&gt;The Boost C++ Libraries ( &lt;a href="http://boost.org/"&gt;http://boost.org/&lt;/a&gt; ) are an open source peer reviewed collection of C++ libraries, aimed at extending and complementing the functionality of the Standard C++ Libraries in several different directions. These libraries are available under the Boost Software License, which is one of the most liberal of open source licenses, and unlike GPL, allows free use of the Boost libraries in both Open Source and proprietary (closed-source) projects.&lt;br /&gt;&lt;br /&gt;Boost represents a collection of libraries some of which are as simple as conversion utilities and some of which are as complex as recursive decent parsers of context-free grammars. A significant number of these libraries are the sort you would like to use everyday - file system manipulation, graph algorithms, heap memory life cycle management, variant data types, regular expressions, conversion utilities, zip and bzip2 file streams, signals and slots mechanisms, etc. Some have compelling features use of which gives your code a level of readability and edge that only C++ can - template driven lambda functions and expression construction. Finally, a good number of these libraries will make their appearance in the upcoming C++ standard (codenamed C++0x). In short, if you are a serious C++ programmer, you cannot afford to stay ignorant about Boost.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;2. &lt;a href="javascript:void(0);" name="purp"&gt;Purpose &amp;amp; Motivation&lt;/a&gt;&lt;/h3&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;Purpose of this short tutorial introduction to Boost is to show the C++ programmer new to Boost, how to download, build and install the Boost libraries on Windows or Linux developer machines, and then get started with writing programs using these libraries.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For building I have chosen Windows and Linux as two representative platforms. I would only be showing how to use the "first-choice" compilers on these platforms - Microsoft C++ (14.0 or above) and GNU g++ (4.1 or above), both of which happen to be free. The reader should, in principle, be able to extrapolate the details to other platforms and compilers.&lt;/p&gt;&lt;p&gt;For the introduction to using the Boost Libraries, the intention is to cover basic use cases of as many Boost libraries as possible - limiting the size of the individual illustrative programs, but giving enough insight into their non-trivial use. The emphasis will be on those libraries that have made it to C++ standard already. But in due course, we will try to cover all the Boost libraries.&lt;br /&gt;&lt;br /&gt;Finally, the sections on Building Boost and Using the Boost Libraries are fairly independent. Therefore the interested reader can read either section ahead of the other.&lt;/p&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;h3&gt;3. &lt;a href="javascript:void(0);" name="bboost"&gt;Building Boost&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Boost distributions are available for download in both source and binary distribution formats. However, just as the best Linux kernel for your box is the one you can build by tweaking the default configuration, similarly the best Boost distribution for your box is the one you build on your box with your preferred compiler. Building Boost involves a fair number of configuration steps, and there are several optional features that you can&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt; turn off&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt; turn on&lt;/a&gt;&lt;/span&gt; in the description below. The optional features are normally described in gray boxes so that they stand-out. This section is a step-by-step, hands-on guide to building the Boost library from source.&lt;/p&gt;&lt;h4&gt;3.1 &lt;a href="javascript:void(0);" name="bboostproc"&gt;The Boost build process&lt;/a&gt;&lt;/h4&gt;&lt;p&gt;&lt;b&gt;3.1.1 Basic Ingredients &lt;/b&gt;&lt;/p&gt;&lt;p&gt;The boost sources are available in the form of a compressed tar ball or a zip archive. Download the Boost sources, [ Latest version 1.35.0 ], from &lt;a href="http://boost.org/"&gt;http://boost.org/&lt;/a&gt; (or &lt;a href="http://sourceforge.net/projects/boost"&gt;http://sourceforge.net/projects/boost&lt;/a&gt;). The size of the download is around 22 MB. Download one of the zip, tar.gz/tgz, or tar.bz2 source archives. On Windows, if you are downloading a tar.gz or tar.bz2 archive, this could get downloaded as boost_&amp;lt;version&amp;gt;.tar.tar and you will need to rename it to boost_&amp;lt;version&amp;gt;.tar.gz or boost_&amp;lt;version&amp;gt;.tar.bz2 as the case may be.&lt;br /&gt;&lt;br /&gt;The Boost source does not require any external configuration before building on a specific platform - so there are no &lt;b&gt;make config&lt;/b&gt; or &lt;b&gt;configure&lt;/b&gt; scripts to run. In fact, there is no &lt;b&gt;make&lt;/b&gt;. Instead, Boost uses a build system that uses Boost-Jam or bjam, a spin-off from the Perforce Jam build utility. Bjam reads jam files just as make reads Makefiles. However, we are not about to go into the details of the syntax of the Jam files.&lt;/p&gt;&lt;p&gt;&lt;b&gt;3.1.2 Build configuration&lt;/b&gt;&lt;/p&gt;&lt;p&gt;The Boost libraries can be built as a vast number of variants, all in one go or separately. We can have &lt;i&gt;&lt;b&gt;debug&lt;/b&gt;&lt;/i&gt; and &lt;i&gt;&lt;b&gt;release&lt;/b&gt;&lt;/i&gt; variants of the same library. For each such library, we can have &lt;i&gt;&lt;b&gt;static&lt;/b&gt;&lt;/i&gt; or &lt;i&gt;&lt;b&gt;shared&lt;/b&gt;&lt;/i&gt; libraries. For each such combination we can have &lt;i&gt;&lt;b&gt;threading-challenged&lt;/b&gt;&lt;/i&gt; or &lt;i&gt;&lt;b&gt;thread-aware&lt;/b&gt;&lt;/i&gt; libraries. And finally, for each such combination, we can have libraries which link statically or dynamically with the C++ Standard and Runtime Libraries of the platform, libraries which link with the debug versions of the C++ Standard and Runtime Libraries of the platform, libraries which have no optimization and inlining (even release libraries), etc. That's a mind-boggling array of combinations. We will stick to a simple set of rules:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Build only thread-aware libraries - not threading-challenged ones. &lt;li&gt;Build only shared libraries - not static ones. &lt;li&gt;Build both debug and release versions of each library.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The build system defaults automatically take care of certain things:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The libraries always link dynamically if the C++ Standard and Runtime libraries are available as shared libraries or DLLs. &lt;li&gt;The libraries always link with the debug versions of the C++ Standard and Runtime libraries for debug builds and release versions of the C++ Standard and Runtime libraries for release builds. &lt;li&gt;Optimization, inlining, etc is turned off for debug builds and done for release builds. &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;In addition, some features of some of the Boost Libraries, and the Boost.Python library itself are not built into the libraries by default but require additional third party libraries. We do not attempt to build the Boost.Python and Boost.MPI libraries in this tutorial, but we do attempt to build all the remaining third-party libraries and use them for the build. &lt;b&gt;All sections which deal with these libraries have a gray background to indicate that these are optional steps and can be skipped for a basic functional build.&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;3.1.3 Optional enhancements&lt;/b&gt; &lt;/p&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;&lt;p&gt;The following third-party, free libraries can be downloaded optionally, in order to support certain features of the Boost libraries which are by default not built. They are platform-agnostic downloads - so a single downloaded archive can be used on both Windows and Linux.&lt;b&gt; If you don't need a particular feature from among these, you need not download the library required for it.&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;zlib - [latest version 1.2.3] from &lt;a href="http://www.zlib.net/"&gt;http://www.zlib.net/&lt;/a&gt;. Small download of around 500 K. Download one of the zip, tar.gz/tgz, or tar.bz2 source archives. On Windows, if you are downloading a tar.gz or tar.bz2 archive, this could get dowloaded as zlib-&amp;lt;version&amp;gt;.tar.tar and you will have to rename it to zlib-&amp;lt;version&amp;gt;.tar.bz2. &lt;b&gt;Needed to support reading from / writing to zipped streams in Boost.IOStreams.&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;li&gt;Download libbz2 [latest version 1.0.5] - from &lt;a href="http://www.bzip.org/downloads.html"&gt;http://www.bzip.org/downloads.html&lt;/a&gt;. Small download of around 850 K. Download one of the zip, tar.gz/tgz, or tar.bz2 source archives. On Windows, if you are downloading a tar.gz or tar.bz2 archive, this could get dowloaded as bzip2-&amp;lt;version&amp;gt;.tar.tar and you will have to rename it to bzip2-&amp;lt;version&amp;gt;.tar.gz. &lt;b&gt;Needed to support reading from / writing to bzip2-compressed streams in Boost.IOStreams.&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;li&gt;Download ICU C++ library [latest stable version 4.0] - from &lt;a href="http://www.icu-project.org/download/"&gt;http://www.icu-project.org/download/&lt;/a&gt; or &lt;a href="ftp://ftp.software.ibm.com/software/globalization/icu"&gt;ftp://ftp.software.ibm.com/software/globalization/icu&lt;/a&gt; (supports anonymous logins). Download the zipped version for Windows. It is a moderately large download - about 12 MB. &lt;b&gt;Needed to support Unicode regular expressions in Boost.Regex.&lt;/b&gt; &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/span&gt;&lt;br /&gt;&lt;h4&gt;3.2 &lt;a href="javascript:void(0);" name="bboostwin"&gt;Building Boost on Windows&lt;/a&gt;&lt;/h4&gt;&lt;p&gt;In order to build Boost 1.35 on Microsoft Windows, you need to have the following software installed.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Microsoft C++ compiler version 12.00.8168 or above. Version 12 was shipped with Visual C++ 6. The recommended version is Microsoft C++ compiler version 15.00. Version 15 ships with Visual C++ 9.0 (2008) and is available for free download in the form of the Microsoft Visual Studio 2008 Xpress Edition and Microsoft Visual C++ 2008 Xpress Edition. &lt;li&gt;Boost Jam v3.1.12 or above. This is a build utility like make based on Perforce Jam. Download the ntx86 version. It's a small download of about 120 KB. Latest version 3.1.16, can be downloaded from [ &lt;a href="http://sourceforge.net/projects/boost"&gt;http://sourceforge.net/projects/boost&lt;/a&gt; ]. &lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;&lt;p&gt;The following third-party, free libraries can be downloaded optionally, in order to support certain features of the Boost libraries that are not built by default. &lt;b&gt;If you don't need a particular feature from among these, you need not download the library required for it.&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Download Expat for Windows [latest version 2.0.1] - from &lt;a href="http://sourceforge.net/projects/expat"&gt;http://sourceforge.net/projects/expat&lt;/a&gt;. Small download of around 535 K. This is a binary which needs to be installed on your build box. &lt;b&gt;Needed to support GraphML vocabulary used by Boost Graph Library.&lt;/b&gt; &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;3.2.1 Build Procedure&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Open a cmd shell. &lt;li&gt;Create a suitable directory for holding all the sources of individual libraries (Boost and others, if any). Make sure you don't have any spaces in its path name. This directory will henceforth be referred to as BOOST_BUILD_ROOT. On my system, I have chosen C:\src as the BOOST_BUILD_ROOT.&lt;br /&gt;&lt;br /&gt;Set the following environment variable:&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;C:\src&amp;gt; set BOOST_BUILD_ROOT=C:\src&lt;br /&gt;&lt;/span&gt;&lt;li&gt;Change your current directory to BOOST_BUILD_ROOT, copy the source archive for Boost into BOOST_BUILD_ROOT and extract it in the current directory, doing the equivalent of "Extract here ...". If everything is successful, you should have the following directory:&lt;br /&gt;&lt;br /&gt;%BOOST_BUILD_ROOT%\boost_&amp;lt;version&amp;gt;&lt;br /&gt;&lt;li&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;Copy each of the source archives for zlib, libbz2 and icu into BOOST_BUILD_ROOT and extract each of them in the current directory, doing the equivalent of "Extract here ..." on each of the archives. If everything is successful, you should have the following directories:&lt;br /&gt;&lt;br /&gt;%BOOST_BUILD_ROOT%\bzip2-&amp;lt;version&amp;gt;&lt;br /&gt;%BOOST_BUILD_ROOT%\zlib-&amp;lt;version&amp;gt;&lt;br /&gt;%BOOST_BUILD_ROOT%\icu&lt;/div&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Copy the boost-jam Windows binary archive into BOOST_BUILD_ROOT and extract it in the current directory, doing the equivalent of "Extract here ...". If everything is successful, you should have the following directory.&lt;br /&gt;&lt;br /&gt;%BOOST_BUILD_ROOT%\boost-jam-&amp;lt;version&amp;gt;-1-ntx86&lt;br /&gt;&lt;br /&gt;For simplicity, rename %BOOST_BUILD_ROOT%\boost-jam-&amp;lt;version&amp;gt;-1-ntx86 to %BOOST_BUILD_ROOT%\bjam. Add this directory to your PATH environment variable.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;C:\src&amp;gt; set PATH=%PATH%:%BOOST_BUILD_ROOT%\bjam&lt;/span&gt;&lt;br /&gt;&lt;li&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;Double click expat-win32bin-&amp;lt;version&amp;gt;.exe to install Expat. During installation, modify the default installation path so that the files are installed at:&lt;br /&gt;%BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;&lt;br /&gt;&lt;br /&gt;Make sure there are no spaces in the directory name as in "Expat 2.0.1", and rename it to "Expat-2.0.1".&lt;br /&gt;&lt;ul&gt;&lt;li&gt;After installation, the %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Bin directory will contain the pre-built binaries. However, a better idea will be to build everything with the compiler of your choice - the one you will use for building Boost. For this, open the expat.dsw VC6 workspace under %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source. Depending on which compiler you intend to use to build Boost, could either open it in VC6 or in a more recent version of Visual C++.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;On VC6: If you open it in VC6, then choose each of the listed projects - elements, expat, expat_static, expatw, expatw_static, outline and xmlfw, in turn. Build the chosen project once in Debug and once in Release configuration. To choose these projects and corresponding configurations, from the menu choose Build --&amp;gt; Set Active Configuration and choose the appropriate Debug or Release configuration. It builds these versions in the directories %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32\bin\Debug or %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32\bin\Release.&lt;br /&gt;&lt;br /&gt;Close the Visual C++ 6 IDE and rename the directory %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32 to %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32-vc6.&lt;br /&gt;&lt;br /&gt;&lt;li&gt;If you open it in a version newer than VC6, then you will be prompted to transform the workspace to a Visual C++ Solution - go ahead and complete this migration. Then choose the "expat" solution and build it. By default, it builds the debug version in the directory %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32\bin\Debug.&lt;br /&gt;&lt;br /&gt;To build the release version, in Visual C++ IDE, from the menu select Build--&amp;gt;Configuration Manager, and then choose the Release configuration from the drop down on the top-left, then close. Rebuild the solution. This time the release version of the binaries are created under the directory %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32\bin\Release.&lt;br /&gt;&lt;br /&gt;Close the Visual C++ IDE and rename the directory %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32 to %BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32-vc&amp;lt;X&amp;gt; where &amp;lt;X&amp;gt; is the version number of Visual Studio - 7 (for 2002), 7.1 (for 2003), 8 (for 2005) and 9 (for 2008).&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;Build the ICU library inside Visual Studio using the solution file allinone.sln present under %BOOST_BUILD_ROOT%\icu\allinone. Make sure that you build in both Debug and Release configurations.&lt;br /&gt;The built binaries will be available under %BOOST_BUILD_ROOT%\icu\bin, and the static and import libraries will be available under %BOOST_BUILD_ROOT%\icu\lib. If you are using ICU version 3 or later, in order to build with Boost, you will need to rename a couple of libraries - icuin.lib to icui18n.lib, icuind.lib to icui18nd.lib, icudtd.lib to icudatad.lib, icuind.pdb to icui18nd.pdb and icudtd.pdb to icudatad.pdb.&lt;/div&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Determine your toolset name, based on the compiler you're using. For Visual Studio, it is msvc. For GNU g++, it is, you guessed it - gcc.&lt;br /&gt;&lt;br /&gt;If you have only one version of Microsoft Visual Studio installed, you could just specify msvc for the toolset. If you have multiple versions, then depending on which version you want to use for the build, you should try one of the following as the toolset name:&lt;/li&gt;&lt;ul&gt;&lt;li&gt;msvc-9.0 (for Visual Studio .NET 2008) &lt;li&gt;msvc-8.0 (for Visual Studio .NET 2005) &lt;li&gt;msvc-7.1 (for Visual Studio .NET 2003) &lt;li&gt;msvc-7.0 (for Visual Studio .NET 2002) &lt;li&gt;msvc-6.0 (for Visual Studio 6)&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Define the following environment variables:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;&lt;li&gt;If you have downloaded and extracted the libbz2 source code, then:&lt;br /&gt;set BZIP2_SOURCE=%BOOST_BUILD_ROOT%\bzip2-&amp;lt;version&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;If you have downloaded and extracted the zlib source code, then:&lt;br /&gt;set ZLIB_SOURCE=%BOOST_BUILD_ROOT%\zlib-&amp;lt;version&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;If you have downloaded and built the eXpat source code, then:&lt;br /&gt;set EXPAT_INCLUDE=%BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\lib&lt;br /&gt;set EXPAT_LIBPATH=%BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32-vc&amp;lt;VCversion&amp;gt;\&amp;lt;variant&amp;gt;&lt;br /&gt;&lt;br /&gt;Here &amp;lt;VCversion&amp;gt; could be 6, 7, 7.1, 8, 9 or any further version depending on the version of VC++ you are using to build Boost.&lt;br /&gt;&amp;lt;variant&amp;gt; is Debug or Release depending on the kind of build you are doing for Boost.&lt;br /&gt;&lt;br /&gt;Finally, make two small changes to your the Jamfile.v2 script found under %BOOST_BUILD_ROOT%\boost_&lt;version&gt;\libs\graph\build. You could backup your Jamfile.v2 file before making the following changes to it.&lt;br /&gt;&lt;br /&gt;1. Look for a line like: &lt;i&gt;optional_reqs += ...&lt;/i&gt;. In the block of directives following it, look for a directive like &lt;i&gt;&amp;lt;find-static-library&amp;gt;expat&lt;/i&gt;. Change it to:&lt;br /&gt;&lt;br /&gt;&amp;lt;find-static-library&amp;gt;libexpatwMT&lt;br /&gt;&lt;br /&gt;2. Look for the line &lt;i&gt;lib boost_graph&lt;/i&gt;. In the block of directives following this - look for a directive like &lt;i&gt;&amp;lt;define&amp;gt;BOOST_GRAPH_NO_LIB=1&lt;/i&gt;. Following this line, add an additional line like this:&lt;br /&gt;&lt;br /&gt;&amp;lt;define&amp;gt;XML_STATIC&lt;br /&gt;&lt;br /&gt;&lt;li&gt;set HAVE_ICU=1&lt;br /&gt;&lt;li&gt;set ICU_PATH=%BOOST_BUILD_ROOT%\icu&lt;br /&gt;&lt;/li&gt;&lt;/div&gt;&lt;/span&gt;&lt;li&gt;set BOOST_BIN_ROOT=%BOOST_BUILD_ROOT%\boost-binary&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Set the MS VC++ environment variables.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;For VC++ 6.0, run the batch file vcvars32.bat under &amp;lt;MSVC_Install_Dir&amp;gt;\Microsoft Visual Studio\VC98\Bin. &amp;lt;MSVC_Install_Dir&amp;gt; is typically C:\Program Files.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;For VC++ 7.0 and 7.1, run the batch file vcvarsall.bat under &amp;lt;MSVCS_Install_Dir&amp;gt;\Microsoft Visual Studio .NET 2002\VC or &amp;lt;MSVCS_Install_Dir&amp;gt;\Microsoft Visual Studio .NET 2003\VC.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;For VC++ 8.0 onwards, run the batch file vcvarsall.bat under &amp;lt;MSVCS_Install_Dir&amp;gt;\Microsoft Visual Studio &amp;lt;version&amp;gt;\VC where &amp;lt;version&amp;gt; is 8 or 9.0.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Change current directory to: %BOOST_BUILD_ROOT%\boost_1_35_0. Make sure that the environment variables you set earlier ((&lt;span class="optdata"&gt;BZIP2_SOURCE, ZLIB_SOURCE, EXPAT_LIBPATH, EXPAT_INCLUDE, HAVE_ICU, ICU_PATH and &lt;/span&gt;BOOST_BIN_ROOT) are available in the cmd shell from where you fire the above command.&lt;br /&gt;&lt;br /&gt;Make sure that the EXPAT_LIBPATH points to the release version of the Expat library.&lt;br /&gt;&lt;br /&gt;set EXPAT_LIBPATH=%BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32-vc&amp;lt;VCversion&amp;gt;\Release&lt;br /&gt;&lt;br /&gt;Fix your installation directory. Say C:\Program Files\Boost. To be on the safe side, figure out the DOS name of any directory which contains spaces, like Program Files. On all my systems it is PROGRA~1 and it may or may not be so on your systems. Set the following environment variable with something like:&lt;br /&gt;&lt;br /&gt;set BOOST_INSTALL_DIR=C:\PROGRA~1\Boost&lt;br /&gt;&lt;br /&gt;Finally, fire the following command to build the "release" variant:&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;bjam threading=multi variant=release link=shared toolset=msvc-9.0 --build-dir="%BOOST_BUILD_ROOT%\boost-binary" --prefix="%BOOST_INSTALL_DIR%" --without-python --without-mpi install&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Reset any environment variables, like EXPAT_LIBPATH appropriately to the debug variant:&lt;br /&gt;set EXPAT_LIBPATH=%BOOST_BUILD_ROOT%\Expat-&amp;lt;version&amp;gt;\Source\win32-vc&amp;lt;VCversion&amp;gt;\Debug&lt;br /&gt;&lt;br /&gt;If you are using Boost 1.45 or later, there is one additional step that needs to be undertaken. You should not use a global bjam, instead build one from the Boost sources using a single script that's also provided with the sources. Do this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;dosprompt&amp;gt; cd %BOOST_BUILD_ROOT%&lt;br /&gt;dosprompt&amp;gt; bootstrap.bat&lt;br /&gt;dosprompt&amp;gt; .\bjam.exe &amp;lt;options above  as explained&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Fire the following command to build the "debug" variant:&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;bjam threading=multi variant=debug link=shared toolset=msvc-9.0 --build-dir="%BOOST_BUILD_ROOT%\boost-binary" --prefix="%BOOST_INSTALL_DIR%" --without-python --without-mpi install&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;After the build, the build area can be cleaned of the intermediate files for each class of variants by replacing the "install" target with "clean" in the above commands.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style="font-size:8;"&gt;We did not show the way to build static Boost libraries, and Boost libraries which link statically with the runtime. To create static libraries, simply replace &lt;span style="font-family:courier new,monospace;"&gt;link=shared&lt;/span&gt; with &lt;span style="font-family:courier new,monospace;"&gt;link=static&lt;/span&gt; in the above commands.&lt;br /&gt;&lt;br /&gt;To build Boost libraries that link statically with the VC runtime, you have to just introduce an additional directive: &lt;span style="font-family:courier new,monospace;"&gt;runtime-link=static&lt;/span&gt;. Note that these libraries themselves can be either static or shared - but they always link statically with the VC Runtime libraries. Because Regex with ICU support necessarily links with ICU's runtime DLLs, the Regex library that links statically to the VC runtime cannot also link with the ICU DLLs. So ICU support is not available for Boost Regex which is statically linked to the VC Runtime. To account for this, you have to set HAVE_ICU and ICU_PATH environment variables to blank before you start the build - otherwise the build fails at the outset.&lt;/span&gt;&lt;br /&gt;&lt;h4&gt;3.3 &lt;a href="javascript:void(0);" name="bboostlin"&gt;Building Boost on Linux (x86)&lt;br /&gt;&lt;/a&gt;&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;In order to build Boost 1.35 on GNU/Linux (on x86), you need to have the following software installed.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;GNU gcc (g++) compiler version 3.4.x or above. The build exercise for this tutorial was done with GNU gcc (g++) compiler version 4.1.2 on OpenSUSE 10.2.&lt;br /&gt;&lt;li&gt;Boost Jam v3.1.12 or above. This is a build utility like make based on Perforce Jam. Download the version for linuxx86. It's a small download of about 65 KB. Latest version 3.1.16, can be downloaded from [ &lt;a href="http://sourceforge.net/projects/boost"&gt;http://sourceforge.net/projects/boost&lt;/a&gt; ].&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;&lt;p&gt;The following third-party, free libraries can be downloaded optionally, in order to support certain features of the Boost libraries. &lt;b&gt;If you don't need a particular feature from among these, you need not download the library required for it.&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Download Expat source archive [latest version 2.0.1] - from &lt;a href="http://sourceforge.net/projects/expat"&gt;http://sourceforge.net/projects/expat&lt;/a&gt;. Small download of around 450 K. &lt;b&gt;Needed to support GraphML vocabulary used by Boost Graph Library.&lt;/b&gt; &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;3.2.1 Build Procedure&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Log on with a non-root account. The build should be done with a non-root account. &lt;li&gt;From the command prompt, create a suitable directory for holding all the sources of individual libraries (Boost and others, if any). Make sure you don't have any spaces in its path name. This directory will henceforth be referred to as BOOST_BUILD_ROOT. On my system, I have chosen ~/boost as the BOOST_BUILD_ROOT.&lt;br /&gt;&lt;br /&gt;Set the following environment variable:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;$ BOOST_BUILD_ROOT=~/boost # or whatever directory your created&lt;br /&gt;$ export BOOST_BUILD_ROOT&lt;br /&gt;&lt;/span&gt;&lt;li&gt;Change your current directory to BOOST_BUILD_ROOT, copy the source archive for Boost into BOOST_BUILD_ROOT and extract it in the current directory. If everything is successful, you should have the following directory:&lt;br /&gt;&lt;br /&gt;$BOOST_BUILD_ROOT/boost_&amp;lt;version&amp;gt;&lt;br /&gt;&lt;br /&gt;On my system, this is how the extraction commands looked:&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ bzip2 -cd boost_1_35_0.tar.bz2 tar xf -&lt;br /&gt;&lt;/span&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;li&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;Copy each of the source archives for zlib, libbz2, icu and expat into $BOOST_BUILD_ROOT, change your current directory to $BOOST_BUILD_ROOT and extract each of them in the current directory. If everything is successful, you should have the following directories:&lt;br /&gt;&lt;br /&gt;$BOOST_BUILD_ROOT/bzip2-&amp;lt;version&amp;gt;&lt;br /&gt;$BOOST_BUILD_ROOT/zlib-&amp;lt;version&amp;gt;&lt;br /&gt;$BOOST_BUILD_ROOT/icu&lt;br /&gt;$BOOST_BUILD_ROOT/expat-&amp;lt;version&amp;gt;&lt;br /&gt;&lt;br /&gt;On my system, this is how the extraction commands looked:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ bzip2 -cd zlib-1.2.3.tar.bz2 tar xf -&lt;br /&gt;$ gzip -cd bzip2-1.0.5.tar.gz tar xf -&lt;br /&gt;$ unzip -o icu4c-4_0-src.zip -d .&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Copy the boost-jam Linux binary archive into BOOST_BUILD_ROOT and extract it in the current directory. If everything is successful, you should have the following directory.&lt;br /&gt;&lt;br /&gt;$BOOST_BUILD_ROOT/boost-jam-&amp;lt;version&amp;gt;-1-linuxx86&lt;br /&gt;&lt;br /&gt;I extracted it this way:&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ gzip -cd boost-jam-3.1.16-1-linuxx86.tgz tar xf -&lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For simplicity, rename $BOOST_BUILD_ROOT/boost-jam-&amp;lt;version&amp;gt;-linuxx86 to $BOOST_BUILD_ROOT/bjam. Add this directory to your PATH environment variable.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;$ PATH=$PATH:$BOOST_BUILD_ROOT/bjam&lt;br /&gt;$ export PATH&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;Building expat:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Change your current directory to $BOOST_BUILD_ROOT/expat-&amp;lt;version&amp;gt; directory and look for a file called configure. If it does not exist, run the following command:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ ./buildconf.sh&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;li&gt;Run the following commands to configure the build:&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ ./configure --prefix=&lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt;$BOOST_BUILD_ROOT/expat&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;The argument &lt;span style="font-family:courier new,monospace;"&gt;--prefix=&lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt;$BOOST_BUILD_ROOT/expat indicates that after building, the libraries, executables, include files, man pages, etc. should be installed under the directory &lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt;&lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt;$BOOST_BUILD_ROOT/expat. If you have write access to other standard directories for third-party installation like /opt, or you know the root password, then you could also do the following:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ ./configure --prefix=/opt&lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt;/expat&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;li&gt;If the above step (configure) is successful, run the following commands to build and install the eXpat library.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ make&lt;/span&gt;&lt;br  style="font-family:courier new,monospace;"&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ make install&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you overrode the prefix option during the configure step with a path which requires root access to write to, then run the following command to do the install itself:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ su -c "make install"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This requires you to know the root password.&lt;br /&gt;&lt;br /&gt;This will build expat and install it to the designated directory (&lt;span style="font-family:courier new,monospace;"&gt;$BOOST_BUILD_ROOT/expat&lt;/span&gt; or /opt/expat). &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/span&gt;&lt;br /&gt;&lt;li&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;Building ICU:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Change your current directory to $BOOST_BUILD_ROOT/icu/source.&lt;br /&gt;&lt;li&gt;Add execute permissions to a couple of scripts:&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ chmod +x install-sh runConfigureICU configure config.sub mkinstalldir \&lt;br /&gt;config.guess config.status&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ dox2unix install-sh runConfigureICU configure &lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt;config.sub&lt;/span&gt;&lt;span style="font-family:courier new,monospace;"&gt; mkinstalldir \&lt;br /&gt;config.guess config.status&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;Run the following command:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ ./runConfigureICU Linux --prefix=/opt/icu # if you have the root password, or write access to /opt&lt;/span&gt;&lt;br /&gt;or&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ ./runConfigureICU Linux --prefix=$BOOST_BUILD_ROOT/icu # if you neither have the root password, nor write access to /opt&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;Upon successful conclusion of runConfigureICU, run the following command:&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ make&lt;/span&gt;&lt;br  style="font-family:courier new,monospace;"&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ make install&lt;/span&gt;&lt;br /&gt;or&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ su -c "make install" # in case the installation target directory, set as prefix above, requires root access to write to&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Determine your toolset name, based on the compiler you're using. For GNU g++, it is gcc.&lt;br /&gt;&lt;li&gt;Define the following environment variables:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Hide&lt;/a&gt;&lt;/span&gt;&lt;span class="optdata" style="display:none"&gt;&lt;a href="javascript:hideLineNums('optdata')"&gt;Optional steps&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="optdata"&gt;&lt;div style="BORDER-RIGHT: black thin solid; PADDING-RIGHT: 1em; BORDER-TOP: black thin solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: silver 0% 50%; PADDING-BOTTOM: 1em; BORDER-LEFT: black thin solid; PADDING-TOP: 1em; BORDER-BOTTOM: black thin solid"&gt;&lt;li&gt;If you have downloaded and extracted the libbz2 source code, then:&lt;br /&gt;BZIP2_SOURCE=$BOOST_BUILD_ROOT/bzip2-&amp;lt;version&amp;gt;&lt;br /&gt;export BZIP2_SOURCE&lt;br /&gt;&lt;br /&gt;&lt;li&gt;If you have downloaded and extracted the zlib source code, then:&lt;br /&gt;ZLIB_SOURCE=$BOOST_BUILD_ROOT/zlib-&amp;lt;version&amp;gt;&lt;br /&gt;ZLIB_SOURCE&lt;br /&gt;&lt;br /&gt;&lt;li&gt;If you have downloaded and built the eXpat source code, then:&lt;br /&gt;EXPAT_INCLUDE=/opt/expat/include&lt;br /&gt;EXPAT_LIBPATH=/opt/expat/lib&lt;br /&gt;or&lt;br /&gt;EXPAT_INCLUDE=$BOOST_BUILD_ROOT/expat/include&lt;br /&gt;EXPAT_LIBPATH=$BOOST_BUILD_ROOT/expat/lib&lt;br /&gt;export EXPAT_INCLUDE EXPAT_LIBPATH&lt;br /&gt;&lt;br /&gt;&lt;li&gt;If you have downloaded and built the ICU source code, then:&lt;br /&gt;HAVE_ICU=1&lt;br /&gt;ICU_PATH=/opt/icu&lt;br /&gt;or&lt;br /&gt;ICU_PATH=$BOOST_BUILD_ROOT/icu&lt;br /&gt;export HAVE_ICU ICU_PATH&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;/div&gt;&lt;/span&gt;&lt;li&gt;BOOST_BIN_ROOT=$BOOST_BUILD_ROOT/boost-binary&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Change current directory to: $BOOST_BUILD_ROOT/boost_1_35_0.&lt;br /&gt;Make sure that the environment variables you set earlier (&lt;span class="optdata"&gt;BZIP2_SOURCE, ZLIB_SOURCE, EXPAT_LIBPATH, EXPAT_INCLUDE, HAVE_ICU, ICU_PATH and &lt;/span&gt;BOOST_BIN_ROOT) are available in the shell you are in.&lt;br /&gt;&lt;br /&gt;Identify a location where you'd like to install the Boost libraries on your system. On my system, I chose /opt/boost, and because I have root access to this box, I could do the following, which is necessary:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;$ su -c "mkdir /opt/boost"&lt;br /&gt;$ su -c "chown arindam:devuser /opt/boost"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If you are using Boost 1.45 or later, there is one additional step that needs to be undertaken. You should not use a global bjam, instead build one from the Boost sources using a single script that's also provided with the sources. Do this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;$ cd $BOOST_BUILD_ROOT&lt;br /&gt;$ ./bootstrap.sh&lt;br /&gt;$ ./bjam &amp;lt;options above  as explained&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Finally, fire the following command to build the "release" variant:&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;./bjam threading=multi variant=release link=shared toolset=gcc --build-dir=$BOOST_BIN_ROOT --prefix=/opt/boost --without-python --without-mpi install&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Fire the following command to build the "debug" variant:&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;./bjam threading=multi variant=debug link=shared toolset=gcc --build-dir=$BOOST_BIN_ROOT --prefix=/opt/boost --without-python --without-mpi install&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you have python-devel and python-tk installed, then you need not add the --without-python directive. Th build process would then generate Boost.Python libraries as well.&lt;br /&gt;&lt;br /&gt;After the build, the object files for each kind of variant can be cleaned by replacing the "stage" target with "clean" in the above commands.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h4&gt;3.4 &lt;a href="javascript:void(0);" name="bboostbin"&gt;Boost Binaries: What's in a name&lt;/a&gt;&lt;/h4&gt;&lt;p&gt;If your build was successful, you should now have a set of DLLs and import libraries built from the source code. If you used the &lt;i&gt;install&lt;/i&gt; target as in the examples above, then the libraries (DLLs, shared objects, import libraries, etc) should all be copied to some convenient location (like /opt/boost/lib or D:\Boost\lib). But the Boost libraries are created in an elaborate directory structure under $BOOST_BIN_ROOT/boost/bin.v2/libs, that needs some scrutiny.&lt;br /&gt;&lt;br /&gt;A large number of the Boost libraries are source-only - in other words all the code in the library is in header files which get included in your own source code directly or through other headers. Needless to say, some of these are typically entirely template code. But a smaller yet significant number of Boost libraries are actually built as shared or static libraries, and need to be linked to your code if you want to use them. For each such library there is a directory created under $BOOST_BIN_ROOT/boost/bin.v2/libs, which goes by the name of the library. You will at least get the following directories:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;date_time &lt;li&gt;filesystem &lt;li&gt;graph &lt;li&gt;iostreams &lt;li&gt;program_options &lt;li&gt;regex &lt;li&gt;serialization &lt;li&gt;signals &lt;li&gt;system &lt;li&gt;test &lt;li&gt;thread &lt;li&gt;wave &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Since we did not choose to build Boost.Python and Boost.MPI, directories for these libraries will not be present. The directory structure under each of these directories looks a bit like this:&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:courier new,monospace;"&gt;&lt;pre&gt;&amp;lt;library&amp;gt;/&lt;br /&gt;---- build/&lt;br /&gt;      ---- &amp;lt;toolset&amp;gt;/&lt;br /&gt;            -------- &amp;lt;variant&amp;gt;/&lt;br /&gt;                      -------- &amp;lt;threading-model&amp;gt;&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;- Assuming that we have used the Microsoft Visual Studio 9, the toolset will be msvc-9.0.&lt;br /&gt;- We have built both debug and release variants.&lt;br /&gt;- We have only built thread-aware libraries. So for the date_time library, based on the above structure, you will get the following directories.&lt;br /&gt;&lt;span style="font-family:courier new,monospace;"&gt;&lt;br /&gt;&lt;pre&gt;date_time/&lt;br /&gt;---- build/&lt;br /&gt;      ---- msvc-9.0/&lt;br /&gt;            ------- debug/&lt;br /&gt;                     ------ threading-multi/&lt;br /&gt;            ------- release/&lt;br /&gt;                     ------ threading-multi/&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;Since we specified "link" as "shared", only DLLs (Windows) or .so.* (Linux) will be build. Of course on Windows, along with each DLL, and Import Library (.lib) for linking is also creared. The dlls, libs and so files are created under the &amp;lt;threading-model&amp;gt; directory which in the above case is threading-multi.&lt;/p&gt;&lt;p&gt;Each .dll, .lib or .so file that is created has a naming convention as well, that needs to be understood:&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;On Unix, each library name starts with "lib". On Windows only static libraries have a name starting with lib, while the import libraries for DLLs and DLLs themselves miss the lib prefix. Barring this, and the file extension, the rest of the naming convention is same on both Windows and Unix.&lt;br /&gt;&lt;br /&gt;&amp;lt;library_name&amp;gt;-&amp;lt;toolset-tag&amp;gt;-&amp;lt;threading-tag&amp;gt;-&amp;lt;abi-tag&amp;gt;-&amp;lt;version&amp;gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;On Windows, this takes the form:&lt;br /&gt;&lt;br /&gt;&amp;lt;library_name&amp;gt;-&amp;lt;toolset-tag&amp;gt;[-&amp;lt;threading-tag&amp;gt;][-&amp;lt;abi-tag&amp;gt;]-&amp;lt;version&amp;gt;.dll&lt;br /&gt;or &amp;lt;library_name&amp;gt;-&amp;lt;toolset-tag&amp;gt;[-&amp;lt;threading-tag&amp;gt;][-&amp;lt;abi-tag&amp;gt;]-&amp;lt;version&amp;gt;.lib&lt;br /&gt;&lt;br /&gt;The parts within brackets [...] represent optional segments that need not be present in all cases.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;On Unix, this takes the form:&lt;br /&gt;&lt;br /&gt;lib&amp;lt;library_name&amp;gt;-&amp;lt;toolset-tag&amp;gt;-&amp;lt;threading-tag&amp;gt;-&amp;lt;abi-tag&amp;gt;-&amp;lt;version&amp;gt;.so[.&amp;lt;version&amp;gt;]&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&amp;lt;library_name&amp;gt; - All library names start with boost_. Thus we have boost_date_time, boost_filesystem, etc.&lt;br /&gt;&lt;li&gt;&amp;lt;toolset-tag&amp;gt; need not be same as &amp;lt;toolset-name&amp;gt; - for the toolsets we have considered, it would be gcc (not g++) on Linux, or vc60, vc71, vc80, vc90, etc. on Windows. &lt;li&gt;&amp;lt;threading-tag&amp;gt; - For multi-threaded builds, it will be mt. For single threaded builds, this part does not appear in the name. &lt;li&gt;&amp;lt;abi-tag&amp;gt; - This is a string of one or more characters. For us, the characters of interest are s, g, d.&lt;br /&gt;s - indicates that the library is statically linked to the C++ Standard Library and Runtime Library.&lt;br /&gt;g - indicates that the debug versions of the standard and runtime support libraries are linked against. On a typical Windows installation you will get a debug version of the runtime libraries to link against, but this need not be the case on Unix boxes.&lt;br /&gt;d - indicates all debug symbols are available in the library, no inlining and optimization has been done while building the code. &lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;p&gt;So the Debug version of the Boost.Regex library on Windows should be called: &lt;b&gt;boost_regex-vc90-mt-gd-1_35.dll&lt;/b&gt; and it should be located under &lt;b&gt;%BOOST_BIN_ROOT%\boost\bin.v2\libs\regex\build\msvc-9.0\debug\threading-multi&lt;/b&gt;.&lt;br /&gt;The corresponding import library should be called &lt;b&gt;boost_regex-vc90-mt-gd-1_35.lib&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;On Linux, the Boost.Regex shared library should be called &lt;b&gt;libboost_regex-gcc-mt-d-1_35.so.1.35.0&lt;/b&gt; and should be located under &lt;b&gt;$BOOST_BIN_ROOT/boost/bin.v2/libs/regex/build/gcc/debug/threading-multi&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;On Windows, the Release version of the Boost.Date_Time library should be called &lt;b&gt;boost_date_time-vc90-mt-1_35.dll &lt;/b&gt;and it should be located under &lt;b&gt;%BOOST_BIN_ROOT%\boost\bin.v2\libs\date_time\build\msvc-9.0\release\threading-multi&lt;/b&gt;. The corresponding import library should be called &lt;b&gt;boost_date_time-vc90-mt-1_35.lib&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;On Linux, the Boost.Date_Time shared library should be called &lt;b&gt;libboost_date_time-gcc-mt-1_35.so.1.35.0&lt;/b&gt; and should be located under &lt;b&gt;$BOOST_BIN_ROOT/boost/bin.v2/libs/date_time/build/gcc/release/threading-multi&lt;/b&gt;.&lt;/p&gt;&lt;br /&gt;&lt;h3&gt;4. &lt;a href="javascript:void(0);" name="boostprog"&gt;Using the Boost Libraries&lt;/a&gt;&lt;/h3&gt;&lt;wbr&gt;&lt;br /&gt;This is a brief section which discusses the way you should set your development environment up to use the Boost headers and link to the libraries effectively. There are several issues that have to be handled - and primarily the fact that there are so many variants of these libraries often causes some confusion. Moreover, there are different issues on Windows and non-Windows systems, so we have separate sections below, addressing these issues.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;4.1. &lt;a href="javascript:void(0);" name="winlink"&gt;Linking to Boost libraries on Windows&lt;/a&gt;&lt;/h4&gt;&lt;br /&gt;On Windows, &lt;i&gt;auto-linking&lt;/i&gt; is supported by most compilers, and by default Boost config headers arrange for Boost libraries to be linked automatically to your code. This is very convenient and you should not override it and try to do manual linking (which is possible but inordinately tedious). However, you still have to understand how your linker options affect which libraries actually get linked.&lt;br /&gt;&lt;br /&gt;On all supported versions of Visual Studio (which is 7.0 onwards), we use &lt;i&gt;one&lt;/i&gt; of the following switches to indicate the kind of linkage we want:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;/MD - Link dynamically with the multi-threaded C Runtime.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;/MDd - Link dynamically with the debug version of the multi-threaded C Runtime.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;/MT - Link statically with multi-threaded C Runtime.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;/MTd - Link statically with the debug version of the multi-threaded C Runtime.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;Depending on which option from the above is chosen during compilation - a different version of a particular Boost library will be chosen for linking automatically. Let us take the example of Boost.Regex. If you check your installation directory, you will see the following DLLs/libs:&lt;br /&gt;&lt;br /&gt;boost_regex-vc90-mt-1_35.dll&lt;br /&gt;boost_regex-vc90-mt-1_35.lib&lt;br /&gt;boost_regex-vc90-mt-gd-1_35.dll&lt;br /&gt;boost_regex-vc90-mt-gd-1_35.lib&lt;br /&gt;&lt;br /&gt;If you actually chose to build static variants of the Boost libraries (unlike what we did), you'd also see the following:&lt;br /&gt;&lt;br /&gt;libboost_regex-vc90-mt-1_35.lib&lt;br /&gt;libboost_regex-vc90-mt-gd-1_35.lib&lt;br /&gt;&lt;br /&gt;Note that while the above are static libraries, they still link to the DLL versions of the MSVC Runtime libraries. Had they linked to the static versions, these would have been named:&lt;br /&gt;&lt;br /&gt;libboost_regex-vc90-mt-s-1_35.lib&lt;br /&gt;libboost_regex-vc90-mt-sgd-1_35.lib&lt;br /&gt;&lt;br /&gt;These are static variants with static runtime-linking - yet another variant. We did not build these either.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;4.1.2. Static linking&lt;/h5&gt;&lt;br /&gt;Now by default, all Boost libraries will link statically with your code. If you have built static libraries, that's fine. So by default, you'd end up linking to &lt;b&gt;libboost_regex-vc90-mt-gd-1_35.lib&lt;/b&gt; in &lt;i&gt;Debug&lt;/i&gt; builds and &lt;b&gt;libboost_regex-vc90-mt-1_35.lib&lt;/b&gt; in &lt;i&gt;Release&lt;/i&gt; builds.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;4.1.2. Dynamic linking&lt;/h5&gt;&lt;br /&gt;But say, like we've done, you have chosen not to build any static linking variants of Boost. What do you do then? Simple - use a preprocessor define to indicate that you want to link dynamically to a particular library. In this case, you would define the symbol &lt;b&gt;BOOST_REGEX_DYN_LINK&lt;/b&gt; using the construct:&lt;br /&gt;&lt;br /&gt;/DBOOST_REGEX_DYN_LINK&lt;br /&gt;&lt;br /&gt;on the command-line. If you want to use Dynamic linking exclusively, simple define &lt;b&gt;BOOST_ALL_DYN_LINK&lt;/b&gt; instead. Beyond that, if you are linking to the Debug version of the VC Runtime, through the use of /MDd, you would link to boost_regex-vc90-mt-gd-1_35.dll via the Import Library boost_regex-vc90-mt-gd-1_35.lib. If you link to the Release / Retail vrsion of the VC Runtime, through the use of /MD, you would link to boost_regex-vc90-mt-1_35.dll via the Import Library boost_regex-vc90-mt-1_35.lib.&lt;br /&gt;&lt;br /&gt;There is a notable exception to this rule - the Boost.Thread library. To link to the boost_thread DLL, define the preprocessor symbol &lt;b&gt;BOOST_THREAD_USE_DLL&lt;/b&gt; on the command-line.&lt;br /&gt;&lt;br /&gt;/DBOOST_THREAD_USE_DLL&lt;br /&gt;&lt;br /&gt;To statically link, define the preprocessor symbol &lt;b&gt;BOOST_THREAD_USE_LIB&lt;/b&gt; on the command-line.&lt;br /&gt;&lt;br /&gt;/DBOOST_THREAD_USE_LIB&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;4.1.2. Static linking plus statically-linked runtime&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;If you use the /MT or /MTd switches, you are saying you want to link statically to the MSVC Runtime libraries. So in this case, you will by default link to libboost_regex-vc90-mt-sgd-1_35.dll (Debug) and libboost_regex-vc90-mt-s-1_35.dll (Release/Retail). Although not prohibited, linking dynamically to the Boost libraries under such circumstances is strictly discouraged. So &lt;i&gt;never&lt;/i&gt; define BOOST_REGEX_DYN_LINK while using /MT or /MTd compiler directives.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:8;"&gt;[In pre-8.0 VC++, that is, upto Visual C++.NET 2003, the Microsoft C++ compiler actually accepted a set of switches - /ML and /MLd - to indicate static linking with a single-threaded runtime environment. On such environments, if you try to link your code with /ML, Boost library would try to link your code to boost_regex-vc90-sgd-1_35.dll or boost_regex-vc90-s-1_35.dll as the case may be. As of Visual C++ 2005 (version 8.0), these variants don't get built any longer.]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;4.2. &lt;a href="javascript:void(0);" name="nixlink"&gt;Linking to Boost libraries on Unix-like systems&lt;/a&gt;&lt;/h4&gt;&lt;wbr&gt;&lt;br /&gt;On Unix, there is no auto-linking and there are no such high level distinctions as Debug and Release builds. The rough equivalent of a Debug build is a build with minimal to no compiler optimization, which includes debugging symbols in binaries. A rough equivalent of Release builds is a build with at least some compiler optimizations and no debugging symbols. Static linking with runtime is rarely used so the -s- and -sgd- variants of the Boost libraries are all but rarely useful.&lt;br /&gt;&lt;br /&gt;An additional complexity to take into account is that on Windows, the Boost libraries silently link a lot of Windows standard libraries for you using Auto-linking. On Unices, you must link these manually and you have to know which ones to link. For all applications that use the multi-threaded variants of the Boost libraries, you have to link with libpthread. Similarly, if you are using Boost.Asio, you need to link with libsocket, (and on Sun Solaris, possibly libnsl, libresolv, etc). You will need to work these out and it's not difficult to do - at least after the first time you get a linker error. Also, unlike on Windows, the base name of a static and a shared library can be same on Unix, barring the extension (.a for static libraries and .so.* for shared libraries).&lt;br /&gt;&lt;br /&gt;Based on the above facts and using gcc as a compiler, one could consider the following cases - once again using the regex library: &lt;ul&gt;&lt;br /&gt;&lt;li&gt;Shared linking with Boost debug libraries:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;g++ -g regextest.cpp -o regextest -I&lt;i&gt;path_to_boost_includes&lt;/i&gt; -L&lt;i&gt;path_to_boost_libraries&lt;/i&gt; -lboost_regex-gcc41-mt-d -lpthread -licuuc -licudata -licui18n&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Shared linking with Boost release libraries:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;g++ -O2 regextest.cpp -o regextest -I&lt;i&gt;path_to_boost_includes&lt;/i&gt; -L&lt;i&gt;path_to_boost_libraries&lt;/i&gt; -lboost_regex-gcc41-mt -lpthread -licuuc -licudata -licui18n&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Static linking with Boost debug libraries:&lt;br /&gt;Here you really have two options. You could choose to link to the static Boost Regex library with static runtime links in which case you can have no ICU support:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;g++ -g regextest.cpp -o regextest -I&lt;i&gt;path_to_boost_includes&lt;/i&gt; -L&lt;i&gt;path_to_boost_libraries&lt;/i&gt; -static -lboost_regex-gcc41-mt-sd -lpthread&lt;/pre&gt;&lt;br /&gt;The above links libpthread statically - which is usually not what you want - so fix that, we have to do this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;g++ -g regextest.cpp -o regextest -I&lt;i&gt;path_to_boost_includes&lt;/i&gt; -L&lt;i&gt;path_to_boost_libraries&lt;/i&gt; -Wl,-Bstatic -lboost_regex-gcc41-mt-sd -Wl,-Bdynamic -lpthread&lt;/pre&gt;&lt;br /&gt;Or you could choose to link to the static Boost Regex library with dynamic runtime links - in which case you can have ICU support:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;g++ -g regextest.cpp -o regextest -I&lt;i&gt;path_to_boost_includes&lt;/i&gt; -L&lt;i&gt;path_to_boost_libraries&lt;/i&gt; -Wl,-Bstatic -lboost_regex-gcc41-mt-d -Wl,-Bdynamic -lpthread -licudata -licui18n -licuuc&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Static linking with Boost release libraries:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;g++ -O2 regextest.cpp -o regextest -I&lt;i&gt;path_to_boost_includes&lt;/i&gt; -L&lt;i&gt;path_to_boost_libraries&lt;/i&gt; -Wl,-Bstatic -lboost_regex-gcc41-mt -Wl,-Bdynamic -lpthread -licudata -licui18n -licuuc&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;If you notice above, the library names are not different between the shared and static linking cases. We do not need to define BOOST_REGEX_DYN_LINK or BOOST_ALL_DYN_LINK. The only differentiator is the &lt;i&gt;-static&lt;/i&gt; directive. The specifics vary from one Unix to another, and even more between different compilers.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-5632940848553059842?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/5632940848553059842/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=5632940848553059842' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/5632940848553059842'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/5632940848553059842'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2008/07/getting-started-with-boost.html' title='Getting Started with Boost'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-1139958762021221660</id><published>2008-06-09T03:08:00.011+05:30</published><updated>2008-07-29T03:16:19.514+05:30</updated><title type='text'>Of Indian authorship: pseudo-intellectual pretentiousness and plagiarism</title><content type='html'>An interesting story on code theft.&lt;br /&gt;While on Indian authorship, plagiarism and stuff, I'd love to share with you a couple of experiences I have had in the recent past.&lt;br /&gt;&lt;br /&gt;I recently came across a book called &lt;a href="http://www.vikaspublishing.com/book_details.asp?Bid=410"&gt;"Encryption: Protecting your Data"&lt;/a&gt; by Whiz Kid &lt;a href="http://en.wikipedia.org/wiki/Ankit_Fadia"&gt;Ankit Fadia&lt;/a&gt;. Now this kid might have been good, at least popular prejudices teach you to think he is - because he is at Stanford. But, I got the impression that his co-author, one babe called Jaya Bhattacharya, did most of the pages for this book - and at the beginning of the book, there is a section on this lass that showers lavish praises on her with lines like - "the constant guidance of Ankit Fadia made her one of the few successful candidates ... This course aroused her interest immensely in the field of Encryption ...". Interesting choice of words, one would have to say. &lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;Here is a piece of &lt;a href="http://www.scripting.com/midas/base64/source.html"&gt;code&lt;/a&gt; for base64 encoding, which appears in the book. Well actually the book does not say so, but this is the source from which they copied the code verbatim into the third chapter of their book (pages 28 - 33) - except for the header comments crediting the original author which were left out in the book. Apparently, this code is written only for Macs, and some of the headers like &amp;lt;appletdefs.h&amp;gt; and &amp;lt;iac.h&amp;gt; are for Mac too - coming from FrontierSDK - some framework exclusively for Macs. Nowhere in the book is anything about Macintosh's and Frontier SDK mentioned and it is not normal to sell a book on Mac programming in India (apart from some Objective C and Cocoa books) - let alone have one by an Indian author. Needless to say, they did not try to compile it even once - or else they would have seen the issues themselves. Otherwise, if we give the authors the benefit of the doubt that they did all their coding on a Mac, then at least that needs to be mentioned in the book.&lt;br /&gt;&lt;br /&gt;Here is another specimen:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp1.blogger.com/_rXplixuNB7o/SExXtwwEpgI/AAAAAAAAABg/_X7XZL3DCGQ/s1600-h/ATgAAAA5UIx9s6DqLmfuk956N-L5LrAHicf-SNsMo8dzrrPk6t0aWUHiaqJUuJGWsC4M_4FSKlxxgXaVy1dmgNUEU2kkAJtU9VCJV4BBc8ltuXolS7TWPnMPc9FrNA.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://bp1.blogger.com/_rXplixuNB7o/SExXtwwEpgI/AAAAAAAAABg/_X7XZL3DCGQ/s400/ATgAAAA5UIx9s6DqLmfuk956N-L5LrAHicf-SNsMo8dzrrPk6t0aWUHiaqJUuJGWsC4M_4FSKlxxgXaVy1dmgNUEU2kkAJtU9VCJV4BBc8ltuXolS7TWPnMPc9FrNA.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5209635312653084162" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;This is from the back cover of a book called Enterprise JavaBeans by Ivan Bayross (and family and friends). The above text should tell you everything - there is not much to read between the lines here.&lt;br /&gt;&lt;br /&gt;So really, this is what it takes to write a book on computing in India.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-1139958762021221660?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/1139958762021221660/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=1139958762021221660' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/1139958762021221660'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/1139958762021221660'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2008/06/interesting-story-on-code-theft.html' title='Of Indian authorship: pseudo-intellectual pretentiousness and plagiarism'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp1.blogger.com/_rXplixuNB7o/SExXtwwEpgI/AAAAAAAAABg/_X7XZL3DCGQ/s72-c/ATgAAAA5UIx9s6DqLmfuk956N-L5LrAHicf-SNsMo8dzrrPk6t0aWUHiaqJUuJGWsC4M_4FSKlxxgXaVy1dmgNUEU2kkAJtU9VCJV4BBc8ltuXolS7TWPnMPc9FrNA.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26653368.post-114673383026051445</id><published>2006-05-04T14:29:00.001+05:30</published><updated>2008-07-29T03:14:56.354+05:30</updated><title type='text'>Shalom !!!</title><content type='html'>It was just before dawn that the El Al Boeing 767 finished its brief hover over the Mediterranean shoreline of Tel Aviv awaiting its turn on the Ben-Gurion tarmac. As it turned around and headed east, flying over apparently the tallest skyscraper in town, the flight first officer announced in Hebrew and English, that we would touch down at Ben Gurion Airport in about ten minutes. I looked at my watch, it showed 6-20 AM and momentarily, my mind went back into thinking what the folks back in India would be doing at this hour. That's a rather boring ponderable, and surely most would be in bed. But my early riser parents would by now have started their day and anxiously awaiting the phone call that I had promised to make "as soon as I land". It was my first international trip, and to Israel, of all places. It was to last more than a month, and for many a fellow Indian traveler, brought here by business, it was foreseeably going to be an anxious time in a land whose only palpable images were of war and conflict, suicide bombings and ambushes - all our pre-travel briefings and assurances notwithstanding. For me, honestly, it was a lot different than that.&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;In my growing years, on numerous occasions, my father spoke to me about the history of the Middle East, with real and anecdotal stories, including his own brush with a piece of Middle Eastern history (the &lt;a href="http://en.wikipedia.org/wiki/Six_day_war"&gt;Six-Day War of 1967&lt;/a&gt;). He seemed to have a passion for the Near East and Semitic history. Each time I heard him on this subject, I felt awed. It would be difficult to put down my finger on what inspired that awe - was it those images of the &lt;a href="http://en.wikipedia.org/wiki/First_Intifada"&gt;Intifada&lt;/a&gt; telecast by a Friday night weekly world news when Shyam Bhatia used to report for NDTV from the hot-bed of Semitic conflict. Or was it the story of the Jews building a nation against all odds and fortifying it like no other people have. Was it that a small sliver of land had, on the shoulders of a great and capable people, stood up to be a technology and military super-power. It was perhaps all of these to a certain extent and a lot more. Whatever it was, I could not help the excitement that rose in me as the plane landed on the runway and pulled up on the tarmac. As I stepped onto the aerobridge, I could feel it - I am in a new and alien land - and it could be a lot of fun.&lt;br /&gt;&lt;br /&gt;After the obligatory and thankfully short immigration rituals, it was our turn to pickup the baggage and look for our escorts. Not even a day earlier, I had spoken to one Mr Bachar over phone, and he had promised he'd bring down his taxi for me and my two companions on that trip. As we waited for someone to appear with a placard carrying our names, so we could know it was Mr Bachar, I got 500 dollars changed to NIS (the New Israeli Shekel) and then looked on at the crowds of young people on the lounge. It didn't matter if it was 4-30 in the morning (by now I had swung the hands of my watch back by two-and-a-half hours) - the Israelis seemed to be having all the fun all the time. As I watched the crowd of young Israeli men and women mingling and embracing each other, celebrating their return to &lt;em&gt;their cherished land&lt;/em&gt; from what ostensibly had been a trip to &lt;em&gt;my&lt;/em&gt; homeland, I kind of got my first glimpse into Israeli life. They seemed to be the kind who'd have used all their alibis to have fun, at any hour. I was happy for them, and I felt a little more assured to be among a happy people.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26653368-114673383026051445?l=shoddykid.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shoddykid.blogspot.com/feeds/114673383026051445/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26653368&amp;postID=114673383026051445' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/114673383026051445'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26653368/posts/default/114673383026051445'/><link rel='alternate' type='text/html' href='http://shoddykid.blogspot.com/2006/05/shalom.html' title='Shalom !!!'/><author><name>Arindam Mukherjee</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry></feed>
