From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,2eac5e4279bf777c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-07-17 07:48:55 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!wn1feed!worldnet.att.net!135.173.83.71!wnfilter1!worldnet-localpost!bgtnsc04-news.ops.worldnet.att.net.POSTED!not-for-mail Message-ID: <3B5450EF.7D82CCF5@worldnet.att.net> From: James Rogers X-Mailer: Mozilla 4.76 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: C++ STL Components and Ada References: <3B5237EC.D54299A7@worldnet.att.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Tue, 17 Jul 2001 14:48:55 GMT NNTP-Posting-Host: 12.86.35.121 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 995381335 12.86.35.121 (Tue, 17 Jul 2001 14:48:55 GMT) NNTP-Posting-Date: Tue, 17 Jul 2001 14:48:55 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:10065 Date: 2001-07-17T14:48:55+00:00 List-Id: 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