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,FREEMAIL_FROM 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-18 08:48:19 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!130.133.1.3!fu-berlin.de!uni-berlin.de!mnch-3e360f9f.pool.mediaways.NET!not-for-mail From: mbenkmann@gmx.de (Matthias Benkmann) Newsgroups: comp.lang.ada Subject: Re: C++ STL Components and Ada Date: Wed, 18 Jul 2001 15:48:08 GMT Message-ID: <3b55aa23.9996894@news.cis.dfn.de> References: <3B5237EC.D54299A7@worldnet.att.net> <3B5450EF.7D82CCF5@worldnet.att.net> <9j24mu$fhq$1@bob.news.rcn.net> <3B54FE7B.9EDF993C@worldnet.att.net> NNTP-Posting-Host: mnch-3e360f9f.pool.mediaways.net (62.54.15.159) X-Trace: fu-berlin.de 995471295 23493676 62.54.15.159 (16 [9078]) X-Newsreader: Forte Free Agent 1.21/32.243 Xref: archiver1.google.com comp.lang.ada:10165 Date: 2001-07-18T15:48:08+00:00 List-Id: On Wed, 18 Jul 2001 03:09:25 GMT, James Rogers wrote: >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. > >Your compiler is completely satisfied with this situation. Nope. >The compiler does not check actual parameter types for template >instantiation. All that is done at run time. This is just plain wrong. I dare you to post some code to support your claim. Look at this: #include using namespace std; class airplane { }; class notAnAirplane { }; int main() { vector va; airplane a; va.insert(va.begin(),a); //OK! insert an airplane notAnAirplane na; va.insert(va.begin(),na); //NOT OK! na is not an airplane /*now try to circumvent type-safety by using pointers*/ vector va2; //a vector of pointer to airplane va2.insert(va2.begin(),&a); //OK! &a is pointer to airplane a /*NOT OK! &na is pointer to notAnAirplane na*/ va2.insert(va2.begin(),&na); }; Ty to find a C++ compiler that will chew it. You won't! Both lines where you try to insert notAnAirplane or pointer to notAnAirplane into a vector of airplane or pointer to airplane give compiler errors. Templates are purely a compile time thing and type checking is static. In Java however, vector only carries Objects and the above would work because there is only vector in Java (although not with this syntax). You have definitely mixed up the 2 languages. >Compare this with the Ada generic instantiation. The compiler >checks the actual parameters against the generic formal parameters. >It also can then check all the return values from functions, and >return types from procedure parameter lists. If there is an error >it will be detected BEFORE you can run your program. You do not >need to rely on testing to find the problems. You do not run the >risk that customers will encounter the problem. You do not >incur the costs of re-releasing the code to provide a fix for the >problem. Same thing for C++. That is why templates are such a Good Thing. MSB ---- By the way: Vacuum cleaners suck!