comp.lang.ada
 help / color / mirror / Atom feed
From: "Hyman Rosen" <hyman.rosen@gmail.com>
Subject: Re: Ada generics
Date: 21 Dec 2006 08:55:03 -0800
Date: 2006-12-21T08:55:03-08:00	[thread overview]
Message-ID: <1166720103.733870.120450@n67g2000cwd.googlegroups.com> (raw)
In-Reply-To: 1166710494.869393.108730@a3g2000cwd.googlegroups.com

markww wrote:
> I'm trying to compare generics in Ada vs C++ templates

There are fundamental differences between the two languages in how
generics work that relate back to how types themselves work.

In C++, a type is a completely static thing. You can read a type
declaration in the code and know everything there is to know about it.
In Ada, types are much more dynamic - they can include information
known only at run type, such as array sizes and discriminant values. (I
don't know Ada, so pardon me if I'm wrong in the Ada details.)
Accordingly, C++ types don't have a lifetime as such, existing for the
life of the program, but Ada types have the same lifetime as other
objects in the scope in which they are declared. Among other things,
this means that C++ types can be assigned unique names at compile time
while Ada types cannot.

Now, imagine you have a generic which wants to take a type parameter.

In C++, because types are fixed and uniquely named, the generic itself
can be instantiated at compile time. The compiler has all the
information it needs. Furthermore, the instantiated generic can also be
uniquely named. This becomes the paradigm of generic instantiation in
C++ - to do it all in the compiler. Thus, generics may take non-type
parameters as well, but they must all be constant (or refer to the
address of a static object). As an additional consequence, C++ is able
to identify instantiations with each other based on identity of the
template parameters so that, for example, the type std::vector<int> in
one file is exactly the same type as std::vector<int> in a second file.
It also makes automatic instantiation of function templates easier,
since again the full set of needed instantiations is known at compile
time, and instantiations with the same set of parameters are identified
with each other. For example, in a function template containing a
static variable, there is one such variable generated for each
different set of instantiation parameters.

In Ada things are different. Because types have lifetimes and contain
runtime information, generic instantiations must also. Furthermore, in
Ada even functions and procedures have lifetimes, because they nest and
have access to outer variables. Now it is not possible to know at
compile time how many generics need to be instantiated, and you cannot
identify a generic instantiation simply by looking at the generic and
its parameters. Each instantiation is separate, and that makes
automatic instantiation of generic functions problematic, because it
may be difficult to know when to do a new instantiation and when to
reuse an existing one. On the other hand, since you are already so
dynamic, there doesn't need to be any restriction on non-type
parameters, and so in Ada you can instantiate generics on
locally-scoped functions and objects.

Because(?) Ada requires explicit instantiations, it has no notion of
generic specializations. This means that in Ada you cannot do
metaprogramming the way you can in C++. In Ada a generic has one and
only one implementation, whereas in C++ a template can be specialized
for different sets of template parameters. On the other hand, Ada is
much more friendly to generic sharing, that is, having a single or a
few compiled functions that can act at runtime as particular
instantiated functions.




  parent reply	other threads:[~2006-12-21 16:55 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-21 14:14 Ada generics markww
2006-12-21 15:42 ` Dmitry A. Kazakov
2006-12-22  7:59   ` Martin Krischik
2006-12-22 16:14     ` Hyman Rosen
2006-12-22  7:59   ` Martin Krischik
2006-12-22 16:41   ` Hyman Rosen
2006-12-22 17:33     ` Markus E Leypold
2006-12-22 18:26       ` Hyman Rosen
2006-12-22 20:59         ` Markus E Leypold
2006-12-22 21:01           ` Markus E Leypold
2006-12-23 14:09           ` Marco
2006-12-25 14:23             ` Hyman Rosen
2006-12-29 14:13               ` Marco
2006-12-25 14:20           ` Hyman Rosen
2006-12-23 11:43     ` Dmitry A. Kazakov
2006-12-25 13:49       ` Hyman Rosen
2006-12-25 14:39         ` Dmitry A. Kazakov
2006-12-26  1:34           ` Hyman Rosen
2006-12-26  9:11             ` Dmitry A. Kazakov
2006-12-26 16:23               ` Hyman Rosen
2006-12-26 19:28                 ` Dmitry A. Kazakov
2006-12-27  1:44                   ` Hyman Rosen
2006-12-27  9:21                     ` Dmitry A. Kazakov
2006-12-27 19:06                       ` Hyman Rosen
2006-12-28 10:59                         ` Dmitry A. Kazakov
2006-12-28 16:29                           ` Hyman Rosen
2006-12-29 11:12                             ` Dmitry A. Kazakov
2006-12-29 14:56                               ` Hyman Rosen
2006-12-28 17:35                           ` Georg Bauhaus
2006-12-29 14:48                             ` Dmitry A. Kazakov
2006-12-29 19:39                               ` Georg Bauhaus
2006-12-30  9:58                                 ` Dmitry A. Kazakov
2006-12-30 14:53                                   ` Georg Bauhaus
2007-01-01 13:00                                     ` Dmitry A. Kazakov
2007-01-02 10:04                                       ` Georg Bauhaus
2007-01-02 11:11                                         ` Dmitry A. Kazakov
2007-01-02 12:33                                           ` Georg Bauhaus
2007-01-02 13:51                                             ` Dmitry A. Kazakov
2007-01-02 14:45                                               ` Georg Bauhaus
2007-01-03 10:10                                                 ` Dmitry A. Kazakov
2007-01-03 14:20                                                   ` Hyman Rosen
2007-01-03 14:55                                                   ` Georg Bauhaus
2007-01-04 10:15                                                     ` Dmitry A. Kazakov
2007-01-03 19:33                                           ` Alexander E. Kopilovich
2007-01-04 10:27                                             ` Dmitry A. Kazakov
2007-01-04 15:00                                               ` Alexander E. Kopilovich
2007-01-05 10:32                                                 ` Dmitry A. Kazakov
2006-12-30  2:25                               ` Randy Brukardt
2006-12-30 10:13                                 ` Dmitry A. Kazakov
2007-01-04  1:09                                   ` Randy Brukardt
2007-01-04 10:07                                     ` Dmitry A. Kazakov
2007-01-05  1:32                                       ` Randy Brukardt
2007-01-05  4:46                                         ` Randy Brukardt
2007-01-05  9:08                                         ` Jean-Pierre Rosen
2007-01-05 20:14                                         ` Georg Bauhaus
2007-01-06  0:14                                           ` Randy Brukardt
2006-12-29  0:09                           ` Randy Brukardt
2006-12-29 11:11                             ` Dmitry A. Kazakov
2006-12-30  2:40                               ` Randy Brukardt
2006-12-21 16:55 ` Hyman Rosen [this message]
2006-12-21 18:22   ` markww
2006-12-22  3:01 ` Steve
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox