    <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  :: Interface Versioning in C++</title>
        <link>https://members.accu.org/index.php/journals/1718</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 #100 - December 2010 + 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/c291/">o100</a>
                    (7)
<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/c291-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c291+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;Interface Versioning in C++</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 31 December 2010 21:34:00 +00:00 or Fri, 31 December 2010 21:34:00 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Updating a widely used DLL interface is non-trivial. Steve Love presents a practical approach.</p>
<p><strong>Body:</strong>&nbsp;<p>Having code that requires more than a single (binary) version of a shared library is what is commonly referred to as 'DLL Hell' in Windows. This can arise in a number of ways, but in particular, a new version of a component is released which must be consumed by clients of the previous version, as well as the new. Just as there are several reasons this might occur, there are varied methods of handling the problem, from the requirement for client code to handle unsightly conversions all the way through to down-right nasty and undefined, or at best, non-portable, behaviour. The approach described here attempts to find a best-of-all-worlds leading to reasonable client code and well-defined, portable implementation.</p>
<h2>The problem</h2>
<p>Consider a shared library<sup><a href="#1">1</a></sup> which is used by multiple clients. Further, that the library is developed and maintained by a team (or company) that is different from those managing the clients. Any change to the library which causes the client code to recompile is a potential deployment nightmare. Release cycles need to be synchronised. Client teams need to co-ordinate the release of the shared library so that all interested clients either upgrade simultaneously, or else are quarantined to remain on the old version. In short, a lot of to-ing and fro-ing from all the parties involved.</p>
<p>The real problem is one of dependency management. If clients didn't need to recompile, there would be no problem with deploying the new version of the library, since (by definition) the client interface of the library must be unchanged. However, this would be an unacceptably onerous restriction on new library versions; the reality is that interfaces do grow stale from time to time, as new features are required.</p>
<h3>The goal</h3>
<p>This article explores how to add new methods to a class interface in such a way that client code need not recompile and redeploy if a new version of the library is released. Other changes to the library cannot be supported easily; deleting a method, or changing a signature (which is equivalent to adding a new method and removing the old one) aren't handled. It's possible to mark an old method as deprecated, allowing client code say one release cycle to change their code, but for the purposes of this article, a version upgrade means adding a new method to an existing type, or adding a new type.</p>
<p>This latter change is easily handled -clients running against a new version have no knowledge of the new type, and so cannot be dependent on it. This also applies to free-standing functions.<sup><a href="#2">2</a></sup> The interesting case is adding a new method to a type already in use.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // part.h   
    #pragma once   
 
    namespace inventory  
    {  
      class part  
      {  
        public:   
          unsigned id() const;  
        private:  
          unsigned num;  
      };  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 1</td>
</tr>
</table>
<p>Consider the code in listing 1 (a concrete library). This code is intended to be part of a shared library called <tt class="code">inventory</tt>. The client code is shown in listing 2.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // app.cpp  
    #include &lt;part.h&gt;  
    #include &lt;iostream&gt;  
    #include &lt;memory&gt;  
 
    // Also link to inventory.lib   
 
    int main()  
    {  
      using namespace std;  
      using namespace inventory;  
 
      unique_ptr&lt; part &gt; p( new part );  
      cout &lt;&lt; p-&gt;id() &lt;&lt; endl;   
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 2</td>
</tr>
</table>
<p>As it stands, adding a new method to the part class would require the client application to recompile against the new header, relink with the new library and redeploy at the same time as the new library is released.</p>
<p>The goal is that when a new method is added to part, none of the above need to happen -the existing released client application will work against the new library version without changes.</p>
<p>Undefined behaviour is the result if the client doesn't recompile, but much worse than that is with most compilers, the undefined behaviour is that the program will still appear to work in this example. The reasons why are explored in 'False hope' (below).</p>
<p>But first things first.</p>
<h3>Untying the knot</h3>
<p>Any change to the library which requires the client to recompile also requires the client to redeploy. Briefly, the changes which might cause that are:</p>
<ol>
<li>Changes to the public member functions</li>
<li>Changes to the base-class list.</li>
<li>Changes to any protected or private member functions</li>
<li>Changes to any data members (presumed to be private in any sane system).</li>
</ol>
<p>For all except number 1, there is a simple solution: introduce a level of indirection. If the client code depended on a true interface type representing a part, instead of a concrete class, then any implementation details would be encapsulated by an implementing type, rather than the interface itself. If any base classes are required by the resulting interface type, then those must also be made into interfaces, because 'Abstractions should not depend upon details. Details should depend upon abstractions.' [Martin96].</p>
<p>In the example shown in listing 1, only the private data needs hiding.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // part.h   
    #pragma once   
    namespace inventory  
    {  
      class part  
      {  
        public:  
          virtual ~part();  
          virtual int id() const = 0;  
      };  
      part * create_part();   
    }   
 
    // part.cpp  
    #include &quot;part.h&quot;   
    namespace  
    {  
      class realpart : public inventory::part   
      {  
        public:  
          realpart();  
          int id() const;  
        private:  
          int num;  
      };  
    }  
    namespace inventory  
    {  
      part * create_part()   
      {  
        return new realpart;  
      }  
    }  
 
    // app.cpp   
    #include &lt;part.h&gt;   
    #include &lt;iostream&gt;   
    #include &lt;memory&gt;   
 
    // Also link to inventory.lib  
    int main()  
    {  
      using namespace std;  
      using namespace inventory;  
      unique_ptr&lt; part &gt; p( create_part() );   
      cout &lt;&lt; p-&gt;id() &lt;&lt; endl;  
    }
</pre></td>
</tr>
<tr>
<td class="title">Listing 3</td>
</tr>
</table>
<p>Listing 3 shows the changes required to make <tt class="code">part</tt> an interface. Notice the simple factory function to create a new instance. This is required since the type that implements the <tt class="code">part</tt> interface, <tt class="code">realpart</tt>, is in effect <span class="c8">private</span> to the library. For brevity, the necessary plumbing to make part a complete interface type, such as handling or prohibiting copying, have been left out.</p>
<p>The public-facing interface for the library now hides all the implementation details from clients, such as data members and private or protected member functions. These are significant changes, because it means that <tt class="code">realpart</tt> can change in any way, as long as it correctly implements the <tt class="code">part</tt> interface, without requiring any recompilation on the part of clients.</p>
<p>The introduction of <tt class="code">part</tt> as an interface is a necessary change to achieve the goal of being able to add a method to that interface, but it is not sufficient to achieve it. Adding a method to the interface <span class="c8">still</span> requires client code to recompile.</p>
<h2>False hope</h2>
<p>Listing 4 shows an update to the part interface - a new method has been added. Even though the client code doesn't use the new method, it must recompile against the new interface definition, and still needs to re-deploy at the same time as the new library is deployed.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // part.h   
    #pragma once  
    #include &lt;string&gt;  
    namespace inventory  
    {  
      class part  
      {  
        public:  
          virtual ~part();  
          virtual int id() const = 0;  
          virtual std::string name() const = 0;  
      };  
      part * create_part();  
    }  
 
    // part.cpp   
    #include &quot;part.h&quot;  
    namespace  
    {  
      class realpart : public inventory::part  
      {  
        public:  
          realpart();  
          int id() const;  
          std::string name() const;  
        private:  
          int num;  
          std::string namestr;  
      };  
    }  
    namespace inventory  
    {  
      part * create_part()  
      {  
        return new realpart;  
      }  
    }  
 
    // app.cpp   
    #include &lt;iostream&gt;  
    #include &lt;memory&gt;  
    #include &lt;inventory.h&gt;  
 
    // Also link to inventory.lib  
    int main()  
    {  
      using namespace std;  
      std::unique_ptr&lt; inventory::part &gt; p(  
         inventory::create_part() );  
      cout &lt;&lt; p-&gt;number() &lt;&lt; endl;  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 4</td>
</tr>
</table>
<p>Suppose for a moment that the new library were deployed, and client code remained as it was - using the old interface. The client code had compiled against a definition of <tt class="code">part</tt> that had only one method, and the deployed library has a <tt class="code">part</tt> type that has two methods.</p>
<h3>Common - but wrong</h3>
<p>It's a fairly common practice in, for example, COM to enhance an existing interface by adding a new method to the end.<sup><a href="#3">3</a></sup></p>
<p>This technique works, but does result in undefined behaviour, due to the one definition rule. However, since COM is defined according to strict compilation rules, and uses IDL to precisely specify object layout, this can be passed-off as platform-specific behaviour -just taking advantage of the code generated by the right compiler. It is not considered good practice in any case; interfaces are supposed to be immutable.</p>
<p>However, COM is not C++ -at its most basic level it is C, and so therefore doesn't use the C++ virtual despatch mechanism.</p>
<p>Even so, adding methods to the end of the interface isn't the real problem.</p>
<p>The real problem is with the implementation class, <tt class="code">realpart</tt>.</p>
<h3>Out of order</h3>
<p>Changing the methods in a pure abstract class in C++ doesn't cause much of a problem at runtime (which is the point in the lifecycle about which we're most concerned here) because at the end of the day, a C++ interface is largely a compile time animal; it's purpose has to do with <span class="c8">type</span>, something the runtime environment knows and cares little about.</p>
<p>In order to see the real problem here, we'll have to start looking at the assembly code. The following examples were compiled with the Microsoft C++ compiler from Visual Studio 2010 (version 16 of cl.exe).</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    class properties  
    {  
    public:  
      virtual int integer() const { return 0; }  
      virtual double floatingpoint() const {  
         return 0; }  
    };  
 
    int main()  
    {  
      properties p;   
    }
</pre></td>
</tr>
<tr>
<td class="title">Listing 5</td>
</tr>
</table>
<p>Listing 5 shows a very simple polymorphic class, <tt class="code">properties</tt>. It exposes two virtual functions, <tt class="code">integer</tt> and <tt class="code">floatingpoint</tt>. The fact that they're inlined is not relevant. Note, however, they are not pure virtual, so a &quot;real&quot; vtable is defined. This file is then compiled with the following command:</p>
<pre class="programlisting">
      cl /EHs /FAs test.cpp  
</pre>
<p><tt class="code">/EHs</tt> means use ordinary C++ exceptions only (synchronous exceptions). <tt class="code">/FAs</tt> causes the compiler to generate an assembly file -test.asm - with inline-source included. The interesting entries can be found by searching for <tt class="code">properties::'vftable</tt> (That's a back-tick character there).</p>
<p>The first such entry shows the general layout of the <tt class="code">properties</tt> class, including RTTI descriptors. The second instance shows the layout of the virtual function table, and should look similar to Figure 1.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    ??_7properties@@6B@ DD FLAT:??_R4properties@@6B@ ; properties::'vftable'  
      DD FLAT:?integer@properties@@EBEHXZ   
      DD FLAT:?floatingpoint@properties@@EBENXZ   

    ; Function compile flags: /Odtp
  
</pre></td>
</tr>
<tr>
<td class="title">Figure 1</td>
</tr>
</table>
<p>This section shows the physical storage for the vtable - the order of entries in it.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    class properties  
    {  
      public:  
        virtual int integer() const { return 0; }  
        virtual double floatingpoint() const {  
          return 0;  
        }  
        virtual void integer( int ){}  
        virtual void floatingpoint( double ){}  
      };  
      int main()  
      {  
        properties p;  
      }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 6</td>
</tr>
</table>
<p>If the <tt class="code">properties</tt> class is now changed to that shown in listing 6, with new method overloads for the same names, and recompiled with the same options, the result is as shown in Figure 2</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    ??_7properties@@6B@ DD FLAT:??_R4properties@@6B@ ; properties::'vftable'  
      DD FLAT:?integer@properties@@EAEXH@Z  
      DD FLAT:?integer@properties@@EBEHXZ  
      DD FLAT:?floatingpoint@properties@@EAEXN@Z  
      DD FLAT:?floatingpoint@properties@@EBENXZ  
    ; Function compile flags: /Odtp  
</pre></td>
</tr>
<tr>
<td class="title">Figure 2</td>
</tr>
</table>
<p>What's really interesting about this result is the order of entries in the vtable. Refer back to listing 6, and compare the order.</p>
<p>What has actually occurred is that functions with the same name are grouped together, even though the actual order of declaration split the functions by getter and setter behaviour. It should be obvious what this means for our proposed method of adding new functions to the bottom of an interface:</p>
<p>It won't work.</p>
<p>It is not safe to depend on the order of the vtable matching the order of declaration. It's therefore unsafe to use a new version of the library without recompiling against its declared classes. Without that recompilation, when the client code calls on a virtual function, the wrong entry in the vtable is invoked (in this example), ultimately calling the wrong function. The results of that are hard to guess. Depending on the vtable order for a particular compiler is, at best, non-portable.</p>
<h2>The true path</h2>
<p>As was previously mentioned, this means that turning the <tt class="code">part</tt> type into an interface isn't sufficient, on its own, to achieve what we need, but it is a necessary step.</p>
<p>The solution hinges around an observation made earlier in this article: adding a new type to a library is easily handled - clients running against a new version have no knowledge of the new type, and so cannot be dependent on it.</p>
<h3>Extending interfaces</h3>
<p>The basic premise of this solution is that instead of adding methods to an interface which is part of a deployed library, the new methods are added to a new interface.</p>
<p>The key to this working is that the new interface inherits publicly from the existing one.</p>
<p>Listing 7 shows the basic interface, <tt class="code">part</tt>, which introduces simple get properties called <tt class="code">number</tt> and <tt class="code">name</tt>, along with how the client code may use it.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // part.h   
    #pragma once  
    #include &lt;string&gt;  
 
    namespace inventory  
    {  
      class part  
      {  
        public:  
          virtual ~part();  
          virtual unsigned id() const = 0;  
          virtual const std::string &amp;  
             name() const = 0;  
      };  
      part * create_part();  
    }  
 
    // app.cpp  
    #include &lt;part.h&gt;   
    #include &lt;iostream&gt;   
    #include &lt;memory&gt;   
 
    // Also link to inventory.lib  
    int main()  
    {  
      using namespace std;  
      using namespace inventory;  
      unique_ptr&lt; part &gt; p( create_part() );  
      cout &lt;&lt; p-&gt;id() &lt;&lt; endl;  
      cout &lt;&lt; p-&gt;name() &lt;&lt; endl;  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 7</td>
</tr>
</table>
<p>New requirements arise to have the properties' values set by the client.</p>
<p>Listing 8 introduces <tt class="code">part_v2</tt>, which extends <tt class="code">part</tt> to add setters for the properties. Note that the names are (deliberately) overloaded, and imported to <tt class="code">part_v2</tt> with <tt class="code">using</tt> statements.<sup><a href="#4">4</a></sup></p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // part.h   
 
    #pragma once  
    #include &lt;string&gt;  
    namespace inventory  
    {  
      class part  
      {  
        public:  
          virtual ~part();  
          virtual unsigned id() const = 0;  
          virtual const std::string &amp;  
             name() const = 0;  
      };  
      class part_v2 : public part  
      {  
        public:  
          using part::id;  
          using part::name;  
          virtual void id( unsigned val ) = 0;  
          virtual void name(  
             const std::string &amp; val ) = 0;  
      };  
      part * create_part();  
    }  
 
    // app.cpp  
    #include &lt;part.h&gt;  
    #include &lt;iostream&gt;  
    #include &lt;memory&gt;  
 
    // Also link to inventory.lib  
    int main()  
    {  
      using namespace std;  
      using namespace inventory;  
      unique_ptr&lt; part_v2 &gt; p(  
         dynamic_cast&lt; part_v2* &gt;( create_part() ) );  
      p-&gt;id( 100 );  
      p-&gt;name( &quot;wingnut&quot; );  
      cout &lt;&lt; p-&gt;id() &lt;&lt; endl;  
      cout &lt;&lt; p-&gt;name() &lt;&lt; endl;  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 8</td>
</tr>
</table>
<p>Existing clients (as shown in listing 7) have no need to recompile, since the object returned from the factory is still an ordinary <tt class="code">part</tt>, which has not changed. New clients wishing to take advantage of the new functionality, such as in listing 8, must compile against the new library.</p>
<h3>A wrong turn</h3>
<p>The interface required to extend the original <tt class="code">part</tt> type has been presented (as <tt class="code">part_v2</tt>), but what of the implementation? The factory must have something to create, and clients must, ultimately, have a real implementing object to do real work.</p>
<p>Listing 9 shows how one might approach the problem. Since clients only ever use the interface, <tt class="code">part</tt>, the details of the implementing class are irrelevant.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    #include &quot;part.h&quot;   
 
    namespace   
    {  
      class realpart : public inventory::part_v2  
      {  
        public:  
          realpart();  
          unsigned id() const;  
          const std::string &amp; name() const;  
          void id( unsigned val );  
          void name( const std::string &amp; val );  
        private:  
          unsigned num;  
          std::string namestr;  
      };  
    }  
 
    namespace inventory  
    {  
      part * create_part()  
      {  
        return new realpart;  
      }  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 9</td>
</tr>
</table>
<p>This code, however, suffers the same problem as the examples in section 2; it is the vtable of the <span class="c8">implementing</span> class that causes the problem, not the interface at all.</p>
<p>As it happens, using the same compiler and flags as before, we can see that this approach actually works in practice. For brevity, the code for <tt class="code">part</tt>, <tt class="code">part_v2</tt>, plus both complete versions of <tt class="code">realpart</tt> have been put in a single file.</p>
<p>Listing 10 shows two versions of the <tt class="code">part</tt> interface, and two independent implementing classes. <tt class="code">realpart_v2</tt> represents the code from listing 9 - a complete implementation of the <tt class="code">part_v2</tt> interface.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    #include &lt;string&gt;  
 
    class part  
    {  
      public:  
        virtual int id() const = 0;  
        virtual const std::string &amp; name() const = 0;  
    };  
    class part_v2 : public part  
    {  
      public:  
        virtual void id( int val ) = 0;  
        virtual void name(  
           const std::string &amp; val ) = 0;  
    };  
 
    class realpart : public part  
    {  
      public:  
        int id() const { return num; }  
        const std::string &amp; name() const {  
           return namestr; }  
      private:  
        int num;  
        std::string namestr;  
    };  
 
    class realpart_v2 : public part_v2  
    {  
      public:  
        int id() const { return num; }  
        const std::string &amp; name() const {  
           return namestr; }  
        void id( int val ) { }  
        void name( const std::string &amp; val ) { }  
      private:  
        int num;  
        std::string namestr;   
    };  
 
    int main()  
    {  
      realpart r1;  
      realpart_v2 r2;  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 10</td>
</tr>
</table>
<p>Compiled with the Microsoft Visual Studio 2010 C++ compiler as before:</p>
<pre class="programlisting">
      cl /EHs /FAs test.cpp  
</pre>
<p>figure 3 shows the vtable layouts corresponding to <tt class="code">realpart</tt> and <tt class="code">realpart_v2</tt>. As you can see, the compiler has helpfully laid the <tt class="code">realpart_v2</tt> vtable out by ensuring that the <span class="c8">derived</span> interface, <tt class="code">part_v2</tt>, appears entirely after the base interface <tt class="code">part</tt>.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    ??_7realpart@@6B@ DD FLAT:??_R4realpart@@6B@ ; realpart::'vftable'  
      DD FLAT:?id@realpart@@UBEHXZ  
      DD FLAT:?name@realpart@@UBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ   
    ; Function compile flags: /Odtp  
 
    ??_7realpart_v2@@6B@ DD FLAT:??_R4realpart_v2@@6B@ ; realpart_v2::'vftable'  
      DD FLAT:?id@realpart_v2@@UBEHXZ  
      DD FLAT:?name@realpart_v2@@UBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ   
      DD FLAT:?id@realpart_v2@@UAEXH@Z  
      DD FLAT:?name@realpart_v2@@UAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z   
    ; Function compile flags: /Odtp  
</pre></td>
</tr>
<tr>
<td class="title">Figure 3</td>
</tr>
</table>
<p>This makes sense: <tt class="code">realpart_v2</tt> derives directly from <tt class="code">part_v2</tt>, which derives from <tt class="code">part</tt>. It would be easy at this point to consider the job done.</p>
<p>It's still not portable however. The code for <tt class="code">realpart_v2</tt> is still dependent upon the implementation specific behaviour of the compiler in laying out the vtable this way. A new version of the same compiler might choose to group the functions in a different way, again resulting in undefined behaviour unless client code recompiles.</p>
<h3>Virtually done</h3>
<p>As with splitting the interface in two so that new methods are added by creating a new interface, so the implementation is split in a similar fashion, and follows the same pattern.</p>
<p>The <tt class="code">part</tt> class interface in listing 11 is identical to that in listing 8, showing the <tt class="code">part_v2</tt> interface deriving from <tt class="code">part</tt>. The implementation of <tt class="code">part_v2</tt> in a new class, <tt class="code">realpart_v2</tt>, which derives not only from <tt class="code">part_v2</tt>, but from the original <tt class="code">realpart</tt> concrete implementation of the <tt class="code">part</tt> interface.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // part.h   
    #pragma once   
    #include &lt;string&gt;  
    namespace inventory  
    {  
      class part  
      {  
        public:  
          virtual ~part();  
          virtual unsigned id() const = 0;  
          virtual const std::string &amp;  
             name() const = 0;  
      };  
      class part_v2 : public part  
      {  
        public:  
          using part::id;  
          using part::name;  
          virtual void id( unsigned val ) = 0;  
          virtual void name(  
             const std::string &amp; val ) = 0;  
      };  
      part * create_part();  
    }  
    // part.cpp   
    #include &quot;part.h&quot;  
    namespace   
    {  
      class realpart : public inventory::part  
      {  
        public:  
          realpart();  
          unsigned id() const;  
          const std::string &amp; name() const;  
        protected:  
          unsigned num;  
          std::string namestr;  
      };  
      class realpart_v2 : public inventory::part_v2,  
         public realpart  
      {  
        public:  
          using part::id;  
          using part::name;  
          void id( unsigned val );  
          void name( const std::string &amp; val );  
      };  
    }  
    namespace inventory  
    {  
      part * create_part()  
      {  
        return new realpart_v2;  
      }  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 11</td>
</tr>
</table>
<p>The <tt class="code">realpart_v2</tt> class uses implementation inheritance to bring in the pure-virtual declarations <span class="c8">and</span> implementations of the <tt class="code">part</tt> interface, and with <tt class="code">using</tt> declarations brings those names into scope to allow them to be overloaded with the new, setter, functions. Finally, the factory function <tt class="code">create_part</tt> is changed to return an instance of the <span class="c8">new</span> implementing class.</p>
<p>In order to achieve the required behaviour, the data members of <tt class="code">realpart</tt> (the base class) have been made protected, since <tt class="code">realpart_v2</tt> inherits from <tt class="code">realpart</tt> and requires access to those members. A (possibly) neater but more verbose way of achieving this would be to add protected data accessors to <tt class="code">realpart</tt>. Since client code has no knowledge of either implementing type, protected data in this instance is not a great problem.</p>
<p>Much more of a problem is the fact that this code will not (or at least, <span class="c8">should</span> not) compile.</p>
<h3>Ambiguity banishment</h3>
<p>Figure 4 shows the problem in stark relief. The relationships between <tt class="code">realpart</tt>, <tt class="code">realpart_v2</tt>, <tt class="code">part</tt> and <tt class="code">part_v2</tt> form the dreaded diamond of multiple inheritance nightmares. The difficulty in compiling comes from the ambiguity between the views of <tt class="code">part</tt> as seen by <tt class="code">realpart_v2</tt>: one via <tt class="code">part_v2</tt>, and another via <tt class="code">realpart</tt>.</p>
<table class="sidebartable">
<tr>
<td><img src="/content/images/journals/ol100/Overload%20WW%20XML%20and%20CSS-6-1-2.gif" alt="** PLEASE DESCRIBE THIS IMAGE **" /></td>
</tr>
<tr>
<td class="title">Figure 4</td>
</tr>
</table>
<p>The solution to this is to use <span class="c8">virtual</span> inheritance, a technique that should be infrequently required, but is essential in this instance.</p>
<p>Listing 12 shows the changes required.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // part.h  
    #pragma once  
    #include &lt;string&gt;  
    namespace inventory  
    {  
      class part  
      {  
        public:  
          virtual ~part();  
          virtual unsigned id() const = 0;  
          virtual const std::string &amp; name() const  
             = 0;  
      };  
      class part_v2 : public virtual part  
      {  
        public:  
          using part::id;  
          using part::name;  
          virtual void id( unsigned val ) = 0;  
          virtual void name(  
             const std::string &amp; val ) = 0;  
      };  
      part * create_part();  
    }  
 
    // part.cpp  
    #include &quot;part.h&quot;  
    namespace  
    {  
      class realpart : public virtual inventory::part  
      {  
        public:  
          realpart();  
          unsigned id() const;  
          const std::string &amp; name() const;  
        protected:  
          unsigned num;  
          std::string namestr;  
      };  
      class realpart_v2   
         : public virtual inventory::part_v2,  
         public realpart  
      {  
        public:  
          using part::id;  
          using part::name;  
          void id( unsigned val );  
          void name( const std::string &amp; val );  
      };  
    }  
    namespace inventory  
    {  
      part * create_part()  
      {  
        return new realpart_v2;  
      }  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 12</td>
</tr>
</table>
<p><tt class="code">part_v2</tt> must virtually inherit from <tt class="code">part</tt>, as must <tt class="code">realpart</tt>. So, indeed, must <tt class="code">realpart_v2</tt>, since it is intended as a base class for a (as yet non-existent) new extension, <tt class="code">realpart_v3</tt>.</p>
<p>This is an ABI-breaking change in <tt class="code">realpart</tt>, causing clients to recompile, because it changes the way the vtables are organised. In order to take advantage of the technique, it is necessary to plan for the future and ensure all classes derive virtually <span class="c8">from the outset</span> to avoid ambiguity.</p>
<p>Virtual inheritance is normally used to avoid ambiguity between the data members of a base class that appears twice in the inheritance family. The ambiguity here is caused not by data members, but by the necessity of overriding pure virtual functions. Without the virtual inheritance, <tt class="code">realpart_v2</tt> remains abstract, since it overrides only <span class="c8">one</span> set of <tt class="code">part</tt>'s functions, which are pure virtual. Virtual inheritance ensures that only one instance of the multiply-inherited base class appears in the derived class.</p>
<h2>Finishing polish</h2>
<p>With the technical problems solved, the necessary infrastructure is in place to achieve the primary goal of allowing the shared library to redeploy without requiring client code to also recompile and redeploy. However, there is more that can still be done to make the necessary client code easier to use.</p>
<h3>Names have power</h3>
<p>Note in listing 12 that the factory function, <tt class="code">create_part</tt>, continues to return a <tt class="code">part</tt> pointer. This cannot change to return a <tt class="code">part_v2</tt>, because that would violate the one-definition rule, morally the same as changing a member function on an interface. Clients of the original library don't care, but clients of the new version (as in listing 8) must cast the result to the new version.</p>
<p>Ideally, clients should be able to use the result of the factory <span class="c8">out of the box</span>, confident that its type is the latest version, and that if the library is updated under their feet, so to speak, it will continue to work as before.</p>
<p>We can, in fact, go further than that.</p>
<p>It would be nice if the new version of the interface could be named the same as the old one. Then, clients who now require the new functionality don't need to find all the places they refer to <tt class="code">part</tt>, and rename to <tt class="code">part_v2</tt>.</p>
<p>We can go further still, and take the explicit responsibility for managing the lifetime of the returned object away from the client.</p>
<h3>Called by a common name</h3>
<p>Since the second version of the interface is <tt class="code">part_v2</tt>, it makes sense to call the first one <tt class="code">part_v1</tt>, and have something else which clients can refer to as <tt class="code">part</tt>. An initial idea might be to make <tt class="code">part</tt> a typedef of whatever the latest version of the <tt class="code">part</tt> interface is, but even better than that, the goal of removing explicit management of lifetime away from the client can be met by making <tt class="code">part</tt> a typedef to a smart pointer. Visual Studio 2010 comes with the right tool for this job as part of its C++0x (actually C++TR1) libraries.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    #include &quot;part_v1.h&quot;  
    #include &lt;memory&gt;  
 
    namespace inventory  
    {  
      typedef part_v1 part_current;  
      typedef std::unique_ptr&lt; part_current &gt; part;  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 13</td>
</tr>
</table>
<p>Listing 13 shows a simple scheme to achieve this. The typedef <tt class="code">part_current</tt> is used by the client to insulate it from the actual name of the latest interface version. When a new version of the part interface is added (e.g. <tt class="code">part_v2</tt>), the <tt class="code">part_current</tt> typedef also needs to change to reflect that.</p>
<h3>Cast-free client</h3>
<p>It has already been pointed out that the factory function used to instantiate a <tt class="code">part</tt> cannot be modified to just return a pointer to whatever the current interface version is. Even using the typedef described above for this still results in a modified function when the typedef changes.</p>
<p>If clients are to be agnostic with regard to the actual version of the <tt class="code">part</tt> interface, then it follows that there needs to be some intermediate place that can sensibly perform the right cast, and return the correct instance to the client.</p>
<p>This is crying out for a simple function template that just performs the right cast on the returned pointer from <tt class="code">create_part()</tt>.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    namespace inventory  
    {  
      INVENTORY_LIB part_v1 * create_part_ptr();  
 
      template&lt; typename type_version &gt;  
      part create_part()  
      {  
        return part( dynamic_cast&lt; type_version * &gt;(  
           create_part_ptr() ) );  
      }  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 14</td>
</tr>
</table>
<p>Listing 14 shows how this can be done. The new <tt class="code">create_part()</tt> function now calls into the renamed <tt class="code">create_part_ptr()</tt> factory which performs the actual instantiation.</p>
<p>Making <tt class="code">create_part</tt> a template neatly sidesteps the one-definition rule violation; a function specialised on a new version of the interface is a <span class="c8">different</span> function, and being a template, is compiled into the client, <span class="c8">not</span> the library. It does mean, however, that clients must still refer to the name of the interface's current version, and this is where the <tt class="code">part_current</tt> typedef comes into play.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    #include &lt;part_factory.h&gt;  
    #include &lt;iostream&gt;  
 
    int main()  
    {  
      using namespace std;  
      using namespace inventory;  
 
      part p = create_part&lt; part_current &gt;();  
      cout &lt;&lt; p-&gt;number() &lt;&lt; endl;  
      cout &lt;&lt; p-&gt;name() &lt;&lt; endl;  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 15</td>
</tr>
</table>
<p>Listing 15 shows how these two facilities are used by the client.</p>
<h3>Dependency management</h3>
<p>The final piece of the puzzle is how to organise the libraries so that the facilities are all available, without placing undue dependency strain on clients. The key to this is in the principle alluded to earlier - that abstractions should not depend upon details.</p>
<p>It's a prime-directive of our craft -separate interface from implementation - and to that end, the library will be split into two parts. One part contains <span class="c8">only</span> the interfaces necessary for clients to refer to objects. The second part, the implementation, also contains the necessary facilities to instantiate objects of the required interfaces.</p>
<p>This separation means that client code that has no need to create objects need depend only on the interfaces themselves.</p>
<h3>Interface-only project</h3>
<p>The main currency of the library here is the <tt class="code">part</tt> type, which is actually an alias for a smart pointer to a specific version of an interface. It therefore makes sense that the definition of the name <tt class="code">part</tt> exists in the context of the interface.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // part_v1.h   
    #include &lt;string&gt;  
    namespace inventory  
    {  
      class INVENTORY_LIB part_v1  
      {  
        public:  
          virtual ~part_v1();  
          virtual unsigned number() const = 0;  
          virtual const std::string &amp; name()   
             const = 0;  
      };  
    }  
 
    // part.h  
    #pragma once  
    #include &quot;part_v1.h&quot;  
    #include &lt;memory&gt;  
    namespace inventory  
    {  
      typedef part_v1 part_current;  
      typedef std::unique_ptr&lt; part_current &gt; part;  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 16</td>
</tr>
</table>
<p>Listing 16 shows the contents of the library. This <span class="c8">interface-only</span> library is also the one that will be used by the most clients, and so should bear the name <tt class="code">inventory</tt>. Client code need only include part.h, and ignore <tt class="code">part_v1</tt> as effectively implementation detail.</p>
<h3>The real thing</h3>
<p>The implementation of the interface, and the means to instantiate it, are the responsibility of the second library.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // realpart.h  
    #pragma once  
 
    #include &lt;part_v1.h&gt;  
 
    namespace inventory_impl  
    {  
      class realpart_v1 : public virtual inventory::part_v1  
      {  
        public:  
          realpart_v1();  
          virtual unsigned number() const;  
          virtual const std::string &amp; name() const;  
 
        private:  
          unsigned num;  
          std::string namestr;  
      };  
    }  
 
    // part_factory.h  
    #include &lt;part.h&gt;  
 
    namespace inventory  
    {  
      INVENTORY_LIB part_v1 * create_part_ptr();  
 
      template&lt; typename type_version &gt;  
      part create_part()  
      {  
        return part( dynamic_cast&lt; type_version * &gt;( create_part_ptr() ) );  
      }  
    }  
 
    // part_factory.cpp  
    #include &quot;realpart.h&quot;  
    #include &lt;part.h&gt;  
 
    namespace inventory  
    {  
      using namespace inventory_impl;  
 
      INVENTORY_LIB part_v1 * create_part_ptr()  
      {  
        return new realpart_v1;  
      }  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 17</td>
</tr>
</table>
<p>Since it's expected to have fewer dependents than the interface library, the implementation library in listing 17 can have a less obvious name, e.g. <tt class="code">inventory_impl</tt>. Note the virtual inheritance in listing 17; even though there is as yet no multiple inheritance occurring, the base must be derived <span class="c8">virtually</span> to avoid breaking changes when a new interface is added.</p>
<h3>Version up!</h3>
<p>When the time comes to add new functionality, four things are required (Listing 18):</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    // part_v2.h  
    #include &quot;part_v1.h&quot;  
    #include &lt;string&gt;  
    namespace inventory  
    {  
      class INVENTORY_LIB part_v2   
         : public virtual part_v1  
      {  
        public:  
          using part_v1::number;  
          using part_v1::name;  
          virtual void number( unsigned ) = 0;  
          virtual void name(  
             const std::string &amp; ) = 0;  
      };  
    }  
 
    // part.h  
    #include &quot;part_v2.h&quot;  
    #include &lt;memory&gt;  
    namespace inventory  
    {  
      typedef part_v2 part_current;  
      typedef std::unique_ptr&lt; part_current &gt; part;  
    }  
 
    // realpart.h  
    #pragma once  
    #include &lt;part_v2.h&gt;  
    namespace inventory_impl  
    {  
      class realpart_v1   
         : public virtual inventory::part_v1  
      {  
        public:  
          realpart_v1();  
          virtual unsigned number() const;  
          virtual const std::string &amp; name() const;  
        protected:  
          unsigned num;  
          std::string namestr;  
      };  
      class realpart_v2   
         : public virtual   inventory::part_v2,  
         public realpart_v1  
      {  
        public:  
          using realpart_v1::id;  
          using realpart_v1::name;  
          virtual void number( unsigned );  
          virtual void name( const std::string &amp; );  
      };  
    }  
 
    // part_factory.cpp  
    #include &quot;realpart.h&quot;  
    #include &lt;part.h&gt;  
 
    namespace inventory  
    {  
      using namespace inventory_impl;  
      INVENTORY_LIB part_v1 * create_part_ptr()   
      {  
        return new realpart_v2;  
      }  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 18</td>
</tr>
</table>
<ol>
<li>Add a new interface to the inventory project</li>
<li>Update the part_current typedef</li>
<li>Add a new class to implement the new interface</li>
<li>Return an instance of the new implementing class from the factory</li>
</ol>
<p>For brevity, the <tt class="code">realpart_v1</tt> and <tt class="code">realpart_v2</tt> code shares the same file.</p>
<p>At this point, the shared library can be released, and existing clients can upgrade at leisure. The reason this works is due to the code in listing 19.</p>
<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">
    #include &lt;part.h&gt;  
 
    namespace inventory  
    {  
      INVENTORY_LIB part_v1 * create_part_ptr();  
      template&lt; typename type_version &gt;  
      part create_part()  
      {  
        return part( dynamic_cast&lt; type_version * &gt;(  
           create_part_ptr() ) );  
      }  
    }  
</pre></td>
</tr>
<tr>
<td class="title">Listing 19</td>
</tr>
</table>
<p>The template function compiled into those clients would effectively be as follows:</p>
<pre class="programlisting">
      part create_part()  
      {  
        return part( dynamic_cast&lt; part_v1 * &gt;(  
           create_part_ptr() ) );  
      }  
</pre>
<p>Even though the new version of the library is returning an object which now derives from <tt class="code">part_v2</tt>, those clients have no knowledge of that fact.</p>
<p>Clients who now compile against the new version of the library effectively compile against this:</p>
<pre class="programlisting">
      part create_part()  
      {  
        return part( dynamic_cast&lt; part_v2 * &gt;(  
           create_part_ptr() ) );  
      }  
</pre>
<p>And so can see the new functionality.</p>
<h2>Justifying the means</h2>
<p>As with many things technical and otherwise, this solution is a trade-off between convenience and effort. The convenience is provided by the fact that the shared library is backwards-compatible with clients who are already deployed, and does not require them to recompile and be re-released with a new library version.</p>
<p>This convenience comes at the expense of the effort requried to understand the interfaces in use, along with the fairly advanced techniques required to make it work portably. Instead of a single point of reference for all the facilities offered by an interface, the user must now follow a chain of base interfaces to determine how to use the whole. Similarly, following the chain of implementing classes is a definite obstacle to comprehending the code.</p>
<p>The use-case for which this code was originally developed was specifically focussed on the deployment dependencies between library and clients, in particular allowing clients of a previous version to continue unchanged when a new library was deployed. The cost of extra complexity in understanding the library was accepted as a necessary one to provide this feature. It is an idiom in common use, however, and understanding the <span class="c8">idiom</span> can help to reduce the complexity of understanding the solution.</p>
<p>The original requirement is all about loosening the coupling between client and library, achieved by judicious use of interfaces, then splitting the library into separate interface and implementation libraries. This separation allows clients to choose whether or not a dependency on the implementation -and the factory to create one -is required. If it is not needed, then the client can restrict their dependency to just the interface library. n</p>
<h2>Acknowledgements</h2>
<p>Many thanks to Roger Orr for identifying the real problem with extending interfaces by adding methods to the end. It was he who spotted that the vtable layout cannot be relied upon to match the declaration order, and who showed me how to find that information from the compiler. Thanks to Pete Goodliffe, Chris Oldwood and Frances Buontempo for providing valuable feedback on early drafts, and to all those who attended the presentation at Skills Matter in London, especially Sam Saariste, James Slaughter and Martin Waplington for making me think harder about it, and for pointing out some of the errors!</p>
<h2>References</h2>
<p class="bibliomixed"><a name="Martin96" id="Martin96"></a> [Martin96] Robert C. Martin, 'The Dependency Inversion Principle', C++ Report, May 1996</p>
<p class="footnote"><a name="#1"></a>1 For the purposes of this discussion, the language is C++ and the platform is Windows and DLLs. However, the principles described also apply to other platforms and languages to a greater or lesser degree. In any case, a stable and consistent ABI between clients and libraries is presumed.</p>
<p class="footnote"><a name="#2"></a>2 Care must be taken with adding a new overload for an existing function, of course!</p>
<p class="footnote"><a name="#3"></a>3 There are many caveats to this regarding changing UUIDs which are not really relevant to this article.</p>
<p class="footnote"><a name="#4"></a>4 In reality, a new header file for <tt class="code">part_v2</tt> would be better than adding the new version to the end of the existing file.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
