Re: Class hierarchy of exceptions (Ada, C++)

From: Peter Koch Larsen (pklspam_at_mailme.dk)
Date: 03/30/05


Date: Wed, 30 Mar 2005 12:03:14 +0200


"Tapio Kelloniemi" <spam17@thack.org> skrev i en meddelelse
news:IQX1e.4368$sO2.1682@reader1.news.jippii.net...
> Ioannis Vranos <ivr@remove.this.grad.com> wrote:
>>Ioannis Vranos wrote:
>>
>>> Actually most compilers provide warnings for lots of stuff and provide
>>> an option to display all warnings (e.g. /Wall), even comparison between
>>> signed and unsigned integers generates a warning. And they can also
>>> treat all warnings as *errors* if you specify so.
>>>
>>> However the default is to display warnings for anything that is not
>>> required by the standard to be treated as an error, and compile it.
>>>
>>>
>>> In most cases, one fixes *all* warnings. However there *are* some cases
>>> where the programmer knows better.
>>>
>>> C++ is enabling by default, which I suppose is the opposite direction of
>>> Ada, and that's why I think we can't understand one another. :-)
>>>
>>>
>>> C++ being less restrictive as the default, doesn't mean a compiler does
>>> not generate lots of warnings!
>
> The question is not only about compiler warnings or errors. Ada (as a
> language) has been designed so that it is possible for the
> compiler to check many mistakes which may cause bad results at run time.
> Ada also makes it easier for the user to notice this kind of errors.
> For example:
>
> procedure X is
>
> type Metres is new Natural;
> type Seconds is new Natural;
>
> M : Metrses := 0;
> S : Seconds := 10;
> begin
> if M < S then -- Error, < is not defined for these types

But now you have problems calculating the velocity, right?

type Velocity is new Natural;
V: Velocity = M/S; // probably a compiler error.
> ...
> end if;
> end X;
>
> This is a bit more verbose than using pure int instead of Metres and
> Seconds, but if I wanted a C++ compiler to check this kind of error, I'm
> afread that the resulting C++ code would be much more verbose.

Not so. There is an excellent library which does exactly what you want -
using templates, of course.
>
> Such mistakes as using a pointer to nothing and writing past the array
> bounds don't often happen in Ada.
What makes you believe they happen regularly in C++?
>
>>An example. First compile with the default behaviour, then with all
>>warnings tu
>>rned on:
>>
>>int main()
>>{
>> int i=0;
>>
>> unsigned j=4;
>>
>> j<i;
>>}
>
> procedure Temp is
> I : Integer := 0;
> J : Natural := 4;
> begin
> I < J;
> end Temp;
>
> Without any warnings:
> # gnatmake temp
> gcc -c temp.adb
> temp.adb:5:05: missing ":="
> gnatmake: "temp.adb" compilation error
>
> Notice how the language prevents doing useless things.
>
> If a replace I < J; with null; the result is:
> gcc -c -gnatg temp.adb
> temp.adb:2:04: warning: "I" is not modified, could be declared constant
> temp.adb:2:04: warning: variable "I" is not referenced
> temp.adb:3:04: warning: "J" is not modified, could be declared constant
> temp.adb:3:04: warning: variable "J" is not referenced
> gnatmake: "temp.adb" compilation error

I see no real difference here between a good-quality C++ compiler and Ada.
>
> --
> Tapio

/Peter



Relevant Pages

  • Re: open source OpenVMS (Re: Oracle-RDB seminar notes)
    ... that the compiler, as well as doing other things needed for the target ... matter which language you are using. ... either the features do not exist (Ada style ... because the design hadn't considered the implications ...
    (comp.os.vms)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > no time there was a fully compiant C compiler available. ... Let's address the Ada side first. ... compiler for C, Ada, or any other language. ... Ada certainly provides facilities useful for writing libraries, ...
    (comp.lang.ada)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > no time there was a fully compiant C compiler available. ... Let's address the Ada side first. ... compiler for C, Ada, or any other language. ... Ada certainly provides facilities useful for writing libraries, ...
    (comp.lang.cpp)
  • Re: Why no Ada.Wide_Directories?
    ... (also keep in mind sometime efficiency is required, and if you want place formalism over efficiency, then you have to sacrifice efficiency, conscientiously). ... If Ada 2012 defines some Design by Contract checks as runtime check, this is not a language flaw, a pragmatic choice. ... Along with that, if a compiler is able to statically check what Ada 2012 designate as runtime check, then nothing in the language definition disallows the compiler to apply all static checks it is able to. ...
    (comp.lang.ada)
  • Re: Current status of Ada?
    ... learning and using this great language. ... The reason why compilers maker can't make their compiler totally free ... I agree with you that most of the links you can find on Ada websites ... AdaCore has contributed a free gnat compiler to ...
    (comp.lang.ada)