comp.lang.ada
 help / color / mirror / Atom feed
From: James Rogers <jimmaureenrogers@worldnet.att.net>
Subject: Re: C++ STL Components and Ada
Date: Tue, 17 Jul 2001 14:48:55 GMT
Date: 2001-07-17T14:48:55+00:00	[thread overview]
Message-ID: <3B5450EF.7D82CCF5@worldnet.att.net> (raw)
In-Reply-To: Pine.BSF.4.21.0107162104100.23396-100000@shell5.ba.best.com

Brian Rogoff wrote:
> 
> On Mon, 16 Jul 2001, James Rogers wrote:
> > Occasionally I read questions from Ada newbies about the presence of
> > Ada equivalents to the C++ STL.
> >
> > After many years of reading about and hearing from C++ programmers that
> > the STL is the best thing since the invention of fire, I decided to
> > look into exactly what the STL is. Ok, I took a little longer than
> > others :-).
> 
> The important thing you seem to be missing in your post is the presence of
> an ancillary type called iterators which are defined over the containers
> and abstract the traversal so that algorithms are decoupled from
> containers. You can do this in Ada too, but it's notationally a bit nicer
> in C++ since the iterators over the commonest sequences map directly to
> pointers.

I considered the issue of iterators for this example. C++ actually uses
pointers for iterators on a Vector. This works for C++ because
internally the C++ vector uses the C-style array. C-style arrays do
are, in many aspects, indistinguishable from arrays.

Ada does not suffer the C confusion between arrays and pointers,
or access types. I chose to use the index number (subtype Positive)
as the iterator. The implementation takes this into accoount.
For instance, refrence to an index outside the range of the internal
array is cleanly handled.

Using subtype Positive for the iterator still satisfies the definition
of a Vector. Addition to the end is constant time (as long as a new
allocation is not required). Insertion to the middle is linear time
O(n) as long as a new allocation is not require. Insertion or 
removal of Vector elements causes previous iterator values to become
unstable. That is, they no longer refer to the same Vector elements.

> I wouldn't say they're the best thing since the invention of fire, but C++
> templates allow all sorts of interesting things, almost like macros, but
> linked with the type system. They provide a crude form of parametric
> polymorphism which doesn't have the overhead associated with typical
> implementations of polymorphic languages. I can easily imagine an Ada-like
> language which steals some nice aspects of C++ templates.
> 

C++ templates also have the disadvantage that their instantiations
cannot
be checked by the compiler. Instead, you must deal with runtime errors
when making simple mistakes such as filling a container with values
of Animal and then trying to read them out as values of Mineral.

I find it ineresting that Java, in its current work to implement a
generic programming capability has rejected the C++ template model.
Instead it has adopted a model very similar to the Ada generic 
model, where instantiation is performed at comile time. The reasons
given for this are stronger type safety and better compatibility with
existing Java Virtual Machines. This model allows the Virtual Machines
to be entirely ignorant of generics. All they see is normal byte
code for fully instantiated classes.

I wondered just where the Ada influence on Java generics came from
until I read a list of contributors to the Java generic design. The 
name Norman Cohen of IBM stood out. Among other things, Norman Cohen
is the author of my favorite Ada text "Ada as a Second Language".

I do not claim that C++ templates are evil. I do claim that they 
are not clearly superior to Ada generics. Each approach has its own
set of strengths and weaknesses.

Jim Rogers
Colorado Springs, Colorado USA



  reply	other threads:[~2001-07-17 14:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-16  0:37 C++ STL Components and Ada James Rogers
2001-07-17  4:26 ` Brian Rogoff
2001-07-17 14:48   ` James Rogers [this message]
2001-07-17 19:47     ` Jon Orris
2001-07-18  3:09       ` James Rogers
2001-07-18 15:48         ` Matthias Benkmann
2001-07-18 18:00         ` Jon Orris
2001-07-19 20:48     ` Ray Blaak
2001-07-20  0:40       ` Brian Rogoff
replies disabled

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