    <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  :: Can C++ Learn from Generics in Ada?</title>
        <link>https://members.accu.org/index.php/articles/272</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>




<div class="xar-mod-head"><span class="xar-mod-title">Design of applications and programs + Overload Journal #67 - Jun 2005</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/articles/">All</a>

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

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c67/">Design</a>
<br />

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

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c145/">67</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c67-145/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c67+145/">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;Can C++ Learn from Generics in Ada?</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 June 2005 05:00:00 +01:00 or Thu, 02 June 2005 05:00:00 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id=
"d0e18"></a>Introduction</h2>
</div>
<p>Although C++ templates afford great flexibility and power in the
language, it is widely recognised that the widespread adoption of
modern template techniques is hampered by &quot;features&quot; of the
implementation of templates [<a href=
"#Heinzmann2004a">Heinzmann2004a</a>, <a href=
"#Heinzmann2004b">Heinzmann2004b</a>]. At the recent ACCU,
conference Stroustrup [<a href=
"#Stroustrup2005">Stroustrup2005</a>] described two proposals
currently before the C++ standards committee for implementing
Concepts in C++. Concepts seek to alleviate the difficulties of
template programming by separating the definition of the template
from its instantiation to a certain extent, in as much as the
contract placed on the types used to instantiate the contract is
explicitly stated, so it can be checked and more meaningful
diagnostics given.</p>
<p>In a question at the end of this presentation, Coplien suggested
that other languages may have models that C++ templates could learn
from, and specifically mentioned Ada generics. This paper will give
an overview of the features of the Ada generics model, from a
personal perspective of a programmer with some experience of Ada,
rather than a language designer. It will show why the author
believes C++ has little to gain from Ada generics.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e34" id="d0e34"></a>Rationale of
Ada</h2>
</div>
<p>The design of the Ada language puts a very strong emphasis on
safety and integrity. It is a language that is particularly
tractable to static analysis by abstract interpretation [<a href=
"#Chapman2001">Chapman2001</a>]. The design of the generic model
carries this on, aiming to provide &quot;<span class="quote">the
security that is present for ordinary, unparameterized program
units; in particular the degree of compilation-time error
checking</span>&quot; [<a href="#Ada83">Ada83</a>].</p>
<p>The main forces in the design of C++ are significantly
different. While no language deliberately courts run time errors,
C++ puts the emphasis more on flexibility and expressiveness
[<a href="#Stroustrup2000">Stroustrup2000</a>]. As a consequence of
these differences, the Ada generic model is very different from the
template model of C++.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e53" id="d0e53"></a>The Ada Generic
Model</h2>
</div>
<p>The model of generic units in Ada allows a high degree of static
type checking to be applied to the generic body alone, independent
of its instantiation. It imposes a contract on the types that can
be used to instantiate the unit. This is a very attractive idea,
and is the reasoning behind the proposals for Concepts in C++.
However, the use of generics in Ada will not fit well with the kind
of ad-hoc specialisation and template reuse that is common in
modern C++.</p>
<p>Two features of the Ada generics mechanism would be particularly
unsuitable for C++, and they are also key to the way Ada achieves
the goal of separating definition and instantiation:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>The need for explicit instantiation;</p>
</li>
<li>
<p>The limited set of formal types that can be used.</p>
</li>
</ol>
</div>
<p>Furthermore, Ada's very strict type model leads to additional
generic parameters being needed that at first sight seem
redundant.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e69" id="d0e69"></a>Explicit
Instantiation</h2>
</div>
<p>The first of these is a tiresome burden for the programmer but
probably not inherent in the generic model. Given the overheads
implied by the syntax, this can be as much typing (and therefore
reading) as writing out the body in full. For example, whereas in
C++ we might do this:</p>
<pre class="programlisting">
int x, y;
some_big_struct a, b;
. . .
swap (x, y);
swap (a, b);
</pre>
<p>The Ada equivalent would be:</p>
<pre class="programlisting">
X, Y : integer;
A, B : Some_Big_Struct;
procedure swap is new swap (integer);
procedure swap is new swap (Some_Big_Struct);
. . . 
swap (a, b);
swap (x, y);
</pre>
<p>This is generally a minor hassle in the idioms of Ada. However,
it clearly makes the idioms of modern C++ impractical.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e82" id="d0e82"></a>Limitations on
Formal Types</h2>
</div>
<p>The most significant drawback of the Ada generics mechanism is
the restrictions on what the generic body can assume from the type
provided to it. The declaration and instantiation of a generic in
Ada takes the following form:</p>
<pre class="programlisting">
generic
   Type T is X;
Package Foo;

Package MyFoo is new Foo (Y)
</pre>
<p>Where <tt class="varname">X</tt> is one of the keywords listed
in the following table and <tt class="varname">Y</tt> is an actual
type argument as listed:</p>
<div class="informaltable">
<table border="1" cellspacing="0">
&lt;colgroup&gt;
&lt;col width=&quot;50%&quot;&gt;
&lt;col width=&quot;50%&quot;&gt;&lt;/colgroup&gt;
&lt;thead&gt;
<tr>
<th align="center">Keyword (<tt class="varname">X</tt>)</th>
<th align="center">Actual parameter (<tt class=
"varname">Y</tt>)</th>
</tr>
&lt;/thead&gt;
&lt;tbody&gt;
<tr>
<td>Digits&lt;&gt;</td>
<td>Any floating point type</td>
</tr>
<tr>
<td>Range&lt;&gt;</td>
<td>Any integer type</td>
</tr>
<tr>
<td>Delta&lt;&gt;</td>
<td>Any fixed point type</td>
</tr>
<tr>
<td>[tagged] Private</td>
<td>Any type, but objects can only be copied and compared</td>
</tr>
<tr>
<td>[tagged] limited private</td>
<td>Any type, but objects can only be compared</td>
</tr>
<tr>
<td>New subtype_mark</td>
<td>Any type derived from subtype_mark</td>
</tr>
&lt;/tbody&gt;
</table>
</div>
<p>The only other permitted types are access (pointer) to one of
those types, or an array of one of those types. As well as
packages, functions and procedures can be declared using the same
syntax. For example, the swap procedure might be declared in Ada
as:</p>
<pre class="programlisting">
generic
   type T is private;
procedure swap (X, Y : in out T);
</pre>
<p>There is no way to access arbitrary components of a record
(struct), nor to call overloaded subprograms<sup>[<a name="d0e148"
href="#ftn.d0e148" id="d0e148">1</a>]</sup> by name matching. The
&quot;new subtype_mark&quot; form does allow access to arbitrary members and
subprograms of the base subtype; this achieves approximately the
same effect as passing an object by base-class reference in C++,
which of course does not require a template at all.</p>
<p>As an example of the limitations this imposes, consider two
record types:</p>
<pre class="programlisting">
Type rec1 is record
  X : Integer;
  Y : Integer;
  Some_Other_Stuff : Foo;
End record;

Type rec2 is record
  Y : Integer;
  X : Integer;
  More_Stuff : Bar;
End record;
</pre>
<p>This is roughly equivalent to the C++:</p>
<pre class="programlisting">
struct Rec1 {
  int X; 
  int Y;
  Foo some_other_stuff;
};

struct rec2 {
  int y;
  int x;
  bar more_stuff;
};
</pre>
<p>Now imagine that the <tt class="varname">X</tt> and <tt class=
"varname">Y</tt> members are semantically equivalent in both
structs, and some duplication is discovered which can be
refactored. In C++, the following is possible:</p>
<pre class="programlisting">
template &lt;typename XandY&gt;
void do_common_thing (XandY&amp; obj) {
  obj.X = getX();
  obj.Y = getY();
}
</pre>
<p>This is not possible in Ada; &quot;something with members called X
and Y&quot; is not in the list above, so when you try to write:</p>
<pre class="programlisting">
Generic
  Type XandY is ???
Procedure do_common_thing (obj : in out XandY);
</pre>
<p>There is nothing that can be put in the ???. The same problem
extends to calling subprograms using arguments of the parameterised
type (the Ada equivalent of calling member functions). The only way
for the generic body to call a subprogram with the parameter type
as an argument is to declare each subprogram required as an
additional generic parameter. Clearly this can become very
long-winded if there are several subprograms called from one
generic.</p>
<p>The Ada standard [<a href="#Ada95">Ada95</a>] sec 12.1 gives the
following example:</p>
<pre class="programlisting">
generic
type Item is private;
with function &quot;*&quot;(U, V : Item) return Item
is &lt;&gt;;
function Squaring(X : Item) return Item;
</pre>
<p>which has to be instantiated like this, assuming that the
package contains an overloaded operator for its user-defined
type:</p>
<pre class="programlisting">
function square is new squaring 
(My_Package.My_Type, My_Package.&quot;*&quot;);
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e187" id="d0e187"></a>Apparently
Redundant Parameters</h2>
</div>
<p>Another source of syntactic tedium is the lack of inference of
parameterized types. This is not actually a result of the generic
model, but arises from the strict type model in Ada. As an
illustration, this code fragment is taken from the Ada 95 reference
manual [<a href="#Ada95">Ada95</a>], section 12.5.4:</p>
<pre class="programlisting">
generic
     type Node is private;
     type Link is access Node;
package P is
     ...
end P;
</pre>
<p>Access is the Ada keyword for pointer; the programmer is forced
to tell the compiler what synonym for &quot;pointer to Node&quot; is going to
be used. Compare this with what might be used in C++:</p>
<pre class="programlisting">
template &lt;typename Node&gt;
class P {
public:
  typedef Node* Link;
  ...
};
</pre>
<p>To a C++ programmer's eyes, the Link parameter in Ada looks
redundant. However, it is not; a new type created as a synonym for
&quot;access Node&quot; is a distinct type and cannot be implicitly converted
from any other similar type. The programmer thus has to tell the
generic in case there is already a new type name in use. In C++ of
course, typedef does not have this effect; it merely introduces a
new spelling for the same type. While there are pros and cons for
this approach, it does impose some lack of flexibility on the use
of generics.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e203" id=
"d0e203"></a>Conclusions</h2>
</div>
<p>The Ada generics model has always been contract based, as
befitting the origins and idioms of the language. This provides for
a high degree of early checking, at the expense of flexibility.
While there may be some general lessons to be drawn from this
approach, the specifics of the mechanism make it far too
restrictive to be of any direct use in C++.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e208" id=
"d0e208"></a>Acknowledgements</h2>
</div>
<p>The author wishes to thank the reviewers for their helpful
comments.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e213" id="d0e213"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Ada83" id="Ada83"></a>
<p class="bibliomixed">[Ada83] &quot;Rationale for the Design of the Ada
Programming Language&quot;, <span class="bibliomisc"><a href=
"http://archive.adaic.com/standards/83rat/html/ratl-12-01.html"
target=
"_top">http://archive.adaic.com/standards/83rat/html/ratl-12-01.html</a></span></p>
</div>
<div class="bibliomixed"><a name="Ada95" id="Ada95"></a>
<p class="bibliomixed">[Ada95] <span class="bibliomisc"><a href=
"http://www.adaic.com/standards/ada95.html" target=
"_top">http://www.adaic.com/standards/ada95.html</a></span></p>
</div>
<div class="bibliomixed"><a name="Chapman2001" id=
"Chapman2001"></a>
<p class="bibliomixed">[Chapman2001] &quot;SPARK and Abstract
Interpretation - a white paper&quot;, <span class="bibliomisc"><a href=
"http://praxis-his.com/pdfs/spark_abstract.pdf" target=
"_top">http://praxis-his.com/pdfs/spark_abstract.pdf</a></span></p>
</div>
<div class="bibliomixed"><a name="Heinzmann2004a" id=
"Heinzmann2004a"></a>
<p class="bibliomixed">[Heinzmann2004a] &quot;The Tale of a Struggling
Template Programmer&quot;, Stefan Heinzmann, <span class=
"citetitle"><i class="citetitle">Overload 61</i></span> June
2004.</p>
</div>
<div class="bibliomixed"><a name="Heinzmann2004b" id=
"Heinzmann2004b"></a>
<p class="bibliomixed">[Heinzmann2004b] &quot;A Template Programmer's
Struggles Resolved&quot;, Stefan Heinzmann and Phil Bass, <span class=
"citetitle"><i class="citetitle">Overload 61</i></span> June
2004.</p>
</div>
<div class="bibliomixed"><a name="Stroustrup2000" id=
"Stroustrup2000"></a>
<p class="bibliomixed">[Stroustrup2000] <span class=
"citetitle"><i class="citetitle">The C++ Programming Language,
Special Edition</i></span>, Bjarne Stroustrup, pub Addison
Wesley.</p>
</div>
<div class="bibliomixed"><a name="Stroustrup2005" id=
"Stroustrup2005"></a>
<p class="bibliomixed">[Stroustrup2005] &quot;Better Support for Generic
Programming&quot;, <span class="bibliomisc"><a href=
"http://www.accu.org/conference/accu2005/kd9fc73n3uj94krmnfcj383jhpaduyivby/Stroustrup%20-%20Better%20Support%20for%20Generic%20Programming.pdf"
target=
"_top">http://www.accu.org/conference/accu2005/kd9fc73n3uj94krmnfcj383jhpaduyivby/Stroustrup%20-%20Better%20Support%20for%20Generic%20Programming.pdf</a></span></p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c3" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e148" href="#d0e148" id=
"ftn.d0e148">1</a>]</sup> Ada distinguishes between Functions,
which return a value and may take only input parameters, and
Procedures, which do not return a value and may take input, output
and in-out parameters. Together they are termed subprograms.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
