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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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-18 11:00:01 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!feed2.news.rcn.net!feed1.news.rcn.net!rcn!not-for-mail From: "Jon Orris" Newsgroups: comp.lang.ada Subject: Re: C++ STL Components and Ada Date: Wed, 18 Jul 2001 14:00:38 -0400 Message-ID: <9j4iq6$ncp$1@bob.news.rcn.net> References: <3B5237EC.D54299A7@worldnet.att.net> <3B5450EF.7D82CCF5@worldnet.att.net> <9j24mu$fhq$1@bob.news.rcn.net> <3B54FE7B.9EDF993C@worldnet.att.net> Reply-To: "Jon Orris" X-Trace: UmFuZG9tSVbxKvZXZHdLx6E7b0tCltE/2t1hfCgrGukbjX6cGAyL3D5en4ytY78Txy5kAERnwJU= X-Complaints-To: abuse@rcn.com NNTP-Posting-Date: 18 Jul 2001 17:59:34 GMT X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 X-Priority: 3 X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MSMail-Priority: Normal Xref: archiver1.google.com comp.lang.ada:10183 Date: 2001-07-18T17:59:34+00:00 List-Id: "James Rogers" wrote in message news:3B54FE7B.9EDF993C@worldnet.att.net... > Jon Orris wrote: > > > > "James Rogers" wrote in message > > news:3B5450EF.7D82CCF5@worldnet.att.net... > >> > 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. > > > > Could you provide an example of what you mean here? I've seen this claim > > before, and have been unable to think of how this would occur. I've never > > had this sort of issue crop up when developing in C++. > > > > Sure. > > All pointers to Objects in C++ are equivalent to class-wide access > values in Ada. A pointer to an object can point to objects of all > classes inheriting from the pointer's object base type. > > When instantiating a template you must specify the class of the > objects to be contained by the generic container class. That > class parameter allows objects of the class and all its > subclasses to be stored in the container class. So far, so good. > > Now, what happens if you instantiate the C++ Vector class with > class Airplane, followed by appropriate additions of Airplane > objects to the container. However, you used the cut and paste > method of programming, so one of your functions extracts data > from the Vector and wants to assign it to a reference to the > String class. This isn't so, unless I'm completely misunderstanding your point. One of the nice things about C++ templates is that they lead to greater type safety. When using C++, many template programming techniques can get you closer to the degree of type safety offered by Ada. In the following sample, assignString and noSuchMethod fail to compile when instantiated. class Airplane { public: Airplane(const char* name) : m_name(name) { } const std::string& GetName() const { return m_name; } private: std::string m_name; }; template void works(const std::vector& vec) { for(std::vector::const_iterator iter = vec.begin(); iter != vec.end(); ++iter) { T* val = *iter; std::cout << val->GetName() << std::endl; } } template void assignString(const std::vector& vec) { for(std::vector::const_iterator iter = vec.begin(); iter != vec.end(); ++iter) { std::string& foo = *iter; } } template void noSuchMethod(const std::vector& vec) { for(std::vector::const_iterator iter = vec.begin(); iter != vec.end(); ++iter) { T* val = *iter; std::cout << val->DoSomething() << std::endl; } } int main() { std::vector planes; // Yes, this leaks memory. planes.push_back(new Airplane("first")); planes.push_back(new Airplane("second")); works( planes ); assignString( planes ); noSuchMethod( planes ); return 0; } C:\dev\test>g++ foo.cpp foo.cpp: In function `void assignString(const vector > &)': foo.cpp:57: instantiated from here foo.cpp:36: conversion from `Airplane *const' to non-scalar type `basic_string, __default_alloc_template >' requested foo.cpp:36: cannot initialize `basic_string, __default_alloc_template > &' from `Airplane *const' foo.cpp: In function `void noSuchMethod(const vector > &)': foo.cpp:58: instantiated from here foo.cpp:46: no matching function for call to `Airplane::DoSomething ()' > Your compiler is completely satisfied with this situation. > The compiler does not check actual parameter types for template > instantiation. All that is done at run time. When you test the > program you find that you have some problems. The compiler always checks parameter types at instantiation. One thing that is a problem with C++ templates is that there are certain bugs that won't show up until a template is instantiated, I.e. template void insane(const std::vector& vec) { Airplane a = 17; } // compiles with insane call commented out. int main() { std::vector planes; // insane( planes ); } I'm still learning Ada, and am not very familiar with generics, so hopefully this is something that Ada would fail to compile altogether. Bwt, lest I give the wrong impression, I'm quite aware of the shortcomings in C++, and the STL. At my prior job, we avoided much of the STL, originally because it wasn't standardized and/or supported on all compilers, then because our in house container library was substantially safer. You had to be actively stupid to cause problems with it. Even with my limited knowledge of Ada, I can see substantial advantages over C++. I used to play a 'game' of C++ vs. Ada. After several hours of tracking down a hideous defect in some departed engineer's code, I'd ask myself: "Would Ada's compile or run time checks have detected this automatically?" 9 out of 10 times, the answer was YES. Jon Orris jonorris@ieee.org