    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: auto_value: Transfer Semantics for Value Types</title>
        <link>https://members.accu.org/index.php/journals/1373</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #79 - Jun 2007 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c224/">79</a>
                    (6)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c224-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c224+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;auto_value: Transfer Semantics for Value Types</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 04 June 2007 11:55:00 +01:00 or Mon, 04 June 2007 11:55:00 +01:00</p>
<p><strong>Summary:</strong>&nbsp;std::auto_ptr has a reputation for causing problems due to its surprising copy/assignment semantics. Richard Harris tries to separate the good ideas from the bad.
</p>
<p><strong>Body:</strong>&nbsp;<p>The problem of eliminating unnecessary copies is one that many programmers have addressed at one time or another. This article proposes an alternative to one of the most common techniques, copy-on-write. We'll begin with a discussion on smart pointers, and why we need more than one type of them. We'll then look at the relationship between smart pointers and the performance characteristics of some simple string implementations, including one that supports copy-on-write. I'll suggest that a different choice of smart pointer better captures our intent, and show that what we were trying to do doesn't achieve half as much as we thought it would.   
  </p><p> 
    Finally, I hope to show that whilst the problem we set out to solve turns out to be a bit of a non-issue, this technique has a side effect that can be exploited to dramatically improve the performance of some complex operations.
  </p><pre class="programlisting">     article::article()
    </pre><p> 
    I'd like to begin by recalling Jackson's Rules of Optimization.
  </p>
		<ul><li> 
    Rule 1: Don't do it.
  </li><li> 
    Rule 2 (for experts only): Don't do it yet.
  </li></ul><p> 
    This is probably a little presumptuous of me, but I'd like to add something:
  </p>
		<ul><li> 
    Harris's Addendum: Nyah, nyah. I can't hear you.
  </li></ul><p> 
    I'll admit it's not very mature, but I think it accurately reflects how we all &lt;sup class=&quot;footnoteref&quot;&gt;really</sup> feel. No matter how much a programmer preaches, like Knuth, that premature optimisation is the root of all evil, I firmly believe that deep down they cannot help but recoil at inefficient code.
  </p><p> 
    Don't believe me? Well, ask yourself which of the following function signatures you'd favour:
  </p><pre class="programlisting">
  void f(std::vector&lt;std::string&gt; strings);
  void f(const std::vector&lt;std::string&gt; &amp;strings);</pre><p> 
    Thought so.
  </p><p> 
    But you mustn't feel bad about it, the desire to write optimal code is a &lt;sup class=&quot;footnoteref&quot;&gt;good</sup> thing. And I can prove it.
  </p><p> 
    In their seminal 2004 paper, 'Universal Limits on Computation', Krauss and Starkman demonstrated that the universe will only be able to process another 1.35x10<sup>120</sup> bits during its lifetime. Count them. Just 1.35x10<sup>120</sup>. If Moore's Law continues to hold true, we'll run out of bits in 600 years.
  </p><p> 
    So we can stop feeling guilty about our oft-criticised drive to optimise, because wasted CPU cycles are accelerating the heat death of the universe.
  </p><p> 
    Will nobody think of the children?
  </p><p> 
    Act responsibly.
  </p><p> 
    Optimise.
  </p><p> 
    OK, that's a little disingenuous. Knuth actually said that in 97% of cases we should forget about small efficiencies. I suspect that we'd all agree that using <tt class="code">const</tt> references by default for value types generally falls into the 3% that we shouldn't ignore. There is, after all, a world of difference between choosing the more efficient of two comparably complex statements and significantly increasing the complexity of your code in the name of a relatively small efficiency gain.
  </p><p> 
    There is a grey area though. Sometimes it really is worth increasing the complexity of your code for relatively small gains. Especially when the particular source of inefficiency occurs regularly within your code base and you can hide that complexity behind a nice tightly defined class interface.
  </p><p> 
    This article is going to take a look at those most profligate of wastrels, temporaries.
  </p><p> 
    But since the direct route rarely has the most interesting views, we're going to set off with a discussion on smart pointers.
  </p><h2>
    auto_ptr
  </h2><p> 
    Let's start by taking a look at the definition of <tt class="code">auto_ptr</tt> (see Listing 1).
  </p>
<table class="sidebartable"><tr><td>
<pre class="programlisting">
 template&lt;typename X&gt;
 class auto_ptr
 {
 public:
 typedef X element_type;
   explicit auto_ptr(X *p = 0) throw();
   auto_ptr(auto_ptr &amp;p) throw();
   template&lt;class Y&gt; auto_ptr(auto_ptr&lt;Y&gt; &amp;p)
      throw();
   auto_ptr(auto_ptr_ref&lt;X&gt; p) throw();
   ~auto_ptr() throw();
   auto_ptr &amp; operator=(auto_ptr &amp;p) throw();
   template&lt;class Y&gt; auto_ptr &amp;
      operator=(auto_ptr&lt;Y&gt; &amp;p) throw();
   auto_ptr &amp; operator=(auto_ptr_ref&lt;X&gt; p)
      throw();
   template&lt;class Y&gt; operator auto_ptr_ref&lt;Y&gt;()
      throw();
   template&lt;class Y&gt; operator auto_ptr&lt;Y&gt;()
      throw();
   X &amp; operator*() const throw();
   X * operator-&gt;() const throw();
   X * get() const throw();
   X * release() throw();
   void reset(X *p = 0) throw();
 private:
   X *x_;
 }; 
 </pre></td></tr><tr><td class="title">Listing 1
 </td></tr></table>
  <p> 
    It's a little bit more complicated than you'd expect isn't it?
  </p><p> 
    This is because, like HAL from Clark's 2001: A Space Odyssey, it has been driven ever so slightly barking mad from being given two competing responsibilities. The first of these is to tie the lifetime of an object to the scope in which it's used.
  </p><p> 
    For example:
  </p><pre class="programlisting">
void
f()
{
  const auto_ptr&lt;T&gt; t(new T);
  //...
} //object is destroyed here</pre><p> 
    The destructor of the <tt class="code">auto_ptr</tt> deletes the object it references, ensuring that it is properly destroyed no matter how we leave the scope.
  </p><p> 
    The second responsibility is to safely transfer objects from one location to another. 
  </p><p> 
    For example:
  </p><pre class="programlisting">
auto_ptr&lt;T&gt;
f()
{
  return auto_ptr&lt;T&gt;(new T);
}

void
g(auto_ptr&lt;T&gt; t)
{
  //...
} //object is destroyed here

void
h()
{
  auto_ptr&lt;T&gt; t;
  t = f(); //ownership is transferred from f here
  g(t);    //ownership is transferred to g here
}</pre><p> 
    The two roles are distinguished by the <tt class="code">const</tt>ness of the <tt class="code">auto_ptr</tt> interface. The <tt class="code">const</tt> member functions of <tt class="code">auto_ptr</tt>&lt;sup class=&quot;footnoteref&quot;&gt; </sup>manage lifetime control, whereas the non-<tt class="code">const</tt> member functions manage ownership transfer.
  </p><p> 
    For example, by making the variable <tt class="code">t</tt> in the function <tt class="code">h</tt> in the previous example <tt class="code">const</tt>, we can ensure that the compiler will tell us if we accidentally try to transfer its ownership elsewhere:
  </p><pre class="programlisting">
void
h()
{
  const auto_ptr&lt;T&gt; t;         
  t = f(); //oops         
  g(t);    //oops       
}</pre><p> 
    And herein lies the problem. We want <tt class="code">const</tt><tt class="code">auto_ptrs</tt> to jealously guard their contents, so we make the arguments to the transfer constructor and assignment operator &lt;sup class=&quot;footnoteref&quot;&gt;non-</sup><tt class="code">const</tt> references. But, unnamed temporaries, such as function return values, can only be bound to <tt class="code">const</tt> references, making it difficult to transfer ownership of an object out of one function and into another.
  </p><p> 
    For example:
  </p><pre class="programlisting">
void
h()
{
  g(f()); //now I'm confused       
}</pre><p> 
    This is where the mysterious <tt class="code">auto_ptr_ref</tt> class comes to our rescue. You'll note that <tt class="code">auto_ptr</tt> has a non-<tt class="code">const</tt> conversion to <tt class="code">auto_ptr_ref</tt> and that there's a conversion constructor that takes an <tt class="code">auto_ptr_ref</tt>. So a non-<tt class="code">const</tt> unnamed temporary can be converted to an <tt class="code">auto_ptr_ref</tt>, which will in turn transfer ownership to an <tt class="code">auto_ptr</tt> via the conversion constructor.
  </p><p> 
    Neat, huh?
  </p><p> 
    Well, perhaps. But we could almost certainly do better by giving each of <tt class="code">auto_ptr</tt>'s personalities its own body. We'll do this by introducing a new type, <tt class="code">scoped_ptr</tt>, to manage object lifetimes and stripping those responsibilities from <tt class="code">auto_ptr</tt>.
  </p><p> 
    The definition of <tt class="code">scoped_ptr</tt> is as shown in Listing 2.
  </p>
  <table class="sidebartable"><tr><td>
  <pre class="programlisting">
template&lt;typename X&gt;
class scoped_ptr
{
public:
  typedef X element_type;

  explicit scoped_ptr(X *p = 0) throw();
  explicit scoped_ptr(
     const auto_ptr&lt;X&gt; &amp;p) throw();
  ~scoped_ptr() throw();

  X &amp; operator*() const throw();
  X * operator-&gt;() const throw();
  X * get() const throw();

  auto_ptr&lt;X&gt; release() throw();
private:
  scoped_ptr(const scoped_ptr &amp;); // not
                                  // implemented
  scoped_ptr &amp; operator=(const scoped_ptr &amp;);
                                //not implemented
  X * x_;
};</pre>
</td></tr><tr><td class="title">Listing 2
 </td></tr></table>
  <p> 
    Those in the know will see the similarity with the boost <tt class="code">scoped_ptr</tt> (www.boost.org).  This is only natural since I pretty much just swiped it from there.
  </p><p> 
    As with the original <tt class="code">auto_ptr</tt>, the constructors take ownership of the objects passed to them and the destructor destroys them. The principal change is that it is no longer legal to copy or assign to <tt class="code">scoped_ptr</tt>s (the unimplemented private <tt class="code">copy</tt> constructor and assignment operator are there to suppress the automatically generated ones).
  </p><p> 
    We can continue to use <tt class="code">scoped_ptr</tt> to tie object lifetime to scope:
  </p><pre class="programlisting">
void
f()
{
  scoped_ptr&lt;T&gt; t(new T);
  //...
} //object is destroyed here</pre><p> 
    But we can no longer use it to transfer ownership:
  </p><pre class="programlisting">
scoped_ptr&lt;T&gt; //oops, no copy constructor
f()
{
  return scoped_ptr&lt;T&gt;(new T);
}
void
g(scoped_ptr&lt;T&gt; t) //oops, no copy constructor
{
  //...
}
void
h()
{
  scoped_ptr&lt;T&gt; t;
  t = scoped_ptr&lt;T&gt;(new T); // oops, no assignment
                            // operator
}</pre><p> 
    Now let's have a look at how giving up responsibility for lifetime control changes <tt class="code">auto_ptr</tt> (Listing 3).
  </p> 
 <table class="sidebartable"><tr><td>
<pre class="programlisting">
template&lt;typename X&gt;
class auto_ptr
{
public:
  typedef X element_type;
  explicit auto_ptr(X *p = 0) throw();
  auto_ptr(const auto_ptr &amp;p) throw();
  template&lt;class Y&gt; auto_ptr(
     const auto_ptr&lt;Y&gt; &amp;p) throw();
  ~auto_ptr() throw();
  const auto_ptr &amp; operator=(
     const auto_ptr &amp;p) const throw();
  template&lt;class Y&gt;
    const auto_ptr &amp; operator=(
       const auto_ptr&lt;Y&gt; &amp;p) const throw();
  X &amp; operator*() const throw();
  X * operator-&gt;() const throw();
  X * release() const throw();
private:
  mutable X *x_;
};
 </pre>
 </td></tr><tr><td class="title">Listing 3
 </td></tr></table>
  <p> 
    OK, so I lied a little.
  </p><p> 
    We haven't so much lost the ability to control object lifetimes with <tt class="code">auto_ptr</tt> as made it a little less attractive. The constructors still take ownership of the objects passed to them and the destructor still destroys them, but holding on to them is difficult.
  </p><p> 
    This is because the object pointer is now &lt;sup class=&quot;footnoteref&quot;&gt;mutable</sup>, allowing it to be changed even through <tt class="code">const</tt> member functions. Usually mutable is reserved for members that can change whilst the object maintains the appearance of <tt class="code">const</tt>ness (caches, for example), an idea typically described as logical <tt class="code">const</tt>ness. Here, to be honest, it's a bit of a hack. We need to tell the compiler to abandon all notions of <tt class="code">const</tt>ness for <tt class="code">auto_ptr</tt>s and unfortunately we can't do that (unnamed temporaries rear their problematic heads again). So we lie to the compiler. We tell it that &quot;no, really, this function is <tt class="code">const</tt>&quot; and use mutability to change the object pointer anyway.
  </p><p> 
    We can still use <tt class="code">auto_ptr</tt> to transfer object ownership from one place to another:
  </p><pre class="programlisting">
auto_ptr&lt;T&gt;
f()
{
  return auto_ptr&lt;T&gt;(new T);
}
void
g(auto_ptr&lt;T&gt; t)
{
  //...
} //object is destroyed here
void
h()
{
  auto_ptr&lt;T&gt; t;
  t = f(); //ownership is transferred from f here
  g(t);    //ownership is transferred to g here       
}</pre><p> 
    But we might run into problems if we try to use it to control object lifetime:
  </p><pre class="programlisting">
T
h()
{
  const auto_ptr&lt;T&gt; t;
  g(t);      //ownership is transferred to g here
  return *t; //oops
} </pre><p> 
    It's precisely because this new <tt class="code">auto_ptr</tt> is so slippery, that I've added a release method to boost's <tt class="code">scoped_ptr</tt>. This enables us to use the <tt class="code">scoped_ptr</tt> to control the lifetime of the object within a function and <tt class="code">auto_ptr </tt>to control its transfer during function return.
  </p><p> 
    For example:
  </p><pre class="programlisting">
auto_ptr&lt;T&gt;
f()
{
  scoped_ptr&lt;T&gt; t;
  //...
  return t.release();
}

void
g()
{
  scoped_ptr&lt;T&gt; t(f());
}</pre><p> 
    I shall keep the name <tt class="code">auto_ptr</tt> for this new ownership transfer pointer despite many reasonable arguments against doing so. Firstly, I believe that <tt class="code">auto_ptr</tt> has strong associations with transfer semantice for most of us. Secondly, and far more importantly, it has led to a much snappier title for this article than the alternative.
  </p><p> 
    Henceforth, therefore, when we refer to <tt class="code">auto_ptr</tt>, we will mean this new version, having the sole responsibility of ownership transfer.
  </p><pre class="programlisting">
shared_ptr</pre><p> 
    Another approach to managing object lifetimes is to allow multiple references to share ownership of an object. This is achieved by keeping the object alive for as long as something is referring to it. 
  </p><p> 
    There are two common techniques used to do this, the correct approach and the easy approach. Guess which one we're going to look at.
  </p><p> 
    That's right. Reference counting.
  </p><p> 
    Reference counting works by keeping a count of the number of active references to an object and deleting it once this count drops to zero. Each time a new reference is taken, the count is incremented and each time a reference is dropped, the count is decremented. This is generally achieved by requiring the referencing entity to explicitly register its interest or disinterest in the object.
  </p><p> 
    The chief advantage of using reference counting to implement shared ownership semantics is that it's relatively simple compared to the alternative.
  </p><p> 
    The chief disadvantage occurs when an object directly or indirectly holds a reference to itself, such as when two objects hold references to each other. In this situation, the reference count will not fall to zero unless one of the objects explicitly drops the reference to the other. In practice, it can be extremely difficult to manually remove mutual references since the ownership relationships can be arbitrarily complex. If you would like to bring a little joy into someone's life, ask a Java programmer about garbage collection.
  </p><p> 
    We can automate much of the book-keeping required for reference counting by creating a class to manage the process for us. In another shameless display of plagiarism I'm going to call this class <tt class="code">shared_ptr</tt> (see Listing 4).
  </p>
 <table class="sidebartable"><tr><td>
<pre class="programlisting">
template&lt;typename X&gt;
class shared_ptr
{
public:
  typedef X element_type;
   shared_ptr() throw();
  template&lt;class Y&gt; explicit shared_ptr(Y * p);
  shared_ptr(const shared_ptr &amp;p) throw();
  template&lt;class Y&gt; shared_ptr(
     const shared_ptr&lt;Y&gt; &amp;p) throw();
  template&lt;class Y&gt; explicit shared_ptr(
     const auto_ptr&lt;Y&gt; &amp;p);
  ~shared_ptr() throw();
  shared_ptr &amp; operator=(
     const shared_ptr &amp;p) throw(); 
  template&lt;class Y&gt;
    shared_ptr &amp; operator=(
       const shared_ptr&lt;Y&gt; &amp;p) throw();
  template&lt;class Y&gt;
    shared_ptr &amp; operator=(
       const auto_ptr&lt;Y&gt; &amp;p) throw();
  X &amp; operator*() const throw();
  X * operator-&gt;() const throw();
  X * get() const throw();
  void reset(X *p = 0) throw();
  bool unique() const throw();
  long use_count() const throw();
private:
  X *x_;
  size_t *refs_;
};
</pre>
 </td></tr><tr><td class="title">Listing 4
 </td></tr></table>
  <p> 
    The reference count is pointed to by the member variable <tt class="code">refs_</tt> and is incremented whenever a <tt class="code">shared_ptr</tt> is copied (either through assignment or construction) and decremented whenever a <tt class="code">shared_ptr</tt> is redirected (either through assignment or reset) or destroyed.
  </p><p> 
    For example:
  </p><pre class="programlisting">
void
f()
{
  shared_ptr&lt;T&gt; t(new T); //*refs_==1          
  {
    shared_ptr&lt;T&gt; u(t); //*refs_==2          
    //...
  } //*refs_==1
  //...       
} //*refs_==0, object is destroyed here</pre><p> 
    Reference counting blurs the distinction between object lifetime control and transfer by allowing many entities to simultaneously &quot;own&quot; an object.
  </p><pre class="programlisting">
shared_ptr&lt;T&gt;
f()
{
  return shared_ptr&lt;T&gt;(new T);
}
 void
g(shared_ptr&lt;T&gt; t) //++*refs_       
{
  //...       
} //--*refs_        

void
h()
{
  shared_ptr&lt;T&gt; t;
  t = f(); //ownership is transferred from f here
  g(t);    //ownership is shared with g here       
}</pre><p> 
    The sequence of events in the above example runs as shown in Figure 1.
  </p>
 <table class="sidebartable"><tr><td>
 <pre class="programlisting">
call h
shared_ptr()

call f
new T
shared_ptr(T *)                     //*refs_=1     
shared_ptr(const shared_ptr &amp;)      //++*refs_    
~shared_ptr                         //--*refs_     
exit f

shared_ptr::operator=(const shared_ptr &amp;)

                                  //++*refs_
~shared_ptr                        //--*refs_
call g
shared_ptr(const shared_ptr &amp;)     //++*refs_     
~shared_ptr                        //--*refs_
exit g

exit h
~shared_ptr                        //--*refs_
 </pre>
</td></tr><tr><td class="title">Figure 1
 </td></tr></table>
  <p> 
    As you can see, at the end of <tt class="code">h</tt>, the reference count is zero and the object is consequently destroyed.
  </p><p> 
    Since the object has more than one owner we must exercise caution when using it or, more specifically, when changing its state.
  </p><p> 
    For example:
  </p><pre class="programlisting">
void
f(shared_ptr&lt;T&gt; t)
{
  //...       
}

void
g()
{
  shared_ptr&lt;T&gt; t(new T);
  shared_ptr&lt;const T&gt; u(t);
  f(t);    //ownership is shared with f here                  
            //state of u is uncertain here
}</pre><p> 
    Of course, this is just pointer aliasing in a spiffy new suit and as such shouldn't come as much of a surprise. I mean, &lt;sup class=&quot;footnoteref&quot;&gt;nobody</sup> makes that mistake these days, do they?
  </p><p> 
    Well, almost nobody.
  </p><p> 
    Well, certainly not standard library vendors.
  </p><p> 
    Well, probably not standard library vendors.
  </p><p> 
    Well, probably not very often.
  </p><h2>
    string
  </h2><p> 
    The last time I saw this problem was in a vendor supplied <tt class="code">std::string</tt>. Well, not this problem exactly, but it was related to incorrect use of reference counted objects. I shan't name names, but it was a company you all know and many of you respect. When I finally tracked down what was causing my program to crash I was stunned.
  </p><p> 
    Now you may be wondering how these sorts of problems could possibly relate to <tt class="code">string</tt>, after all it's a value type not an object type. The reason is that this particular <tt class="code">string</tt> used a common optimisation technique known as copy-on-write, or COW for short, and this technique relies upon reference counting.
  </p><p> 
    Next time, we'll take a look at <tt class="code">string</tt> and the implications of the COW optimisation.</p><h2>
    #include
  </h2><p> 
    Krauss and Starkman. <i>Universal Limits on Computation</i> (arXiv:astro-ph/0404510 v2, 2004).
  </p><p> 
    Clark, <i>2001: A Space Odyssey</i> (Hutchinson, 1968).
  </p><h2>
    Acknowledgements
  </h2><p> 
    With thanks to Kevlin Henney for his review of this article and Astrid Osborn, Keith Garbutt and Niclas Sandstrom for proof reading it.
  </p></p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
