comp.lang.ada
 help / color / mirror / Atom feed
From: mbenkmann@gmx.de (Matthias Benkmann)
Subject: Re: C++ STL Components and Ada
Date: Wed, 18 Jul 2001 15:48:08 GMT
Date: 2001-07-18T15:48:08+00:00	[thread overview]
Message-ID: <3b55aa23.9996894@news.cis.dfn.de> (raw)
In-Reply-To: 3B54FE7B.9EDF993C@worldnet.att.net

On Wed, 18 Jul 2001 03:09:25 GMT, James Rogers
<jimmaureenrogers@worldnet.att.net> wrote:

>Jon Orris wrote:
>> 
>> "James Rogers" <jimmaureenrogers@worldnet.att.net> 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 <vector>

using namespace std;

class airplane
{
};

class notAnAirplane
{
};

int main()
{
  vector<airplane> 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<airplane*> 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<Object> 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!



  reply	other threads:[~2001-07-18 15: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
2001-07-17 19:47     ` Jon Orris
2001-07-18  3:09       ` James Rogers
2001-07-18 15:48         ` Matthias Benkmann [this message]
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