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-Thread: 103376,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Thread: 115aec,703c4f68db81387d X-Google-Thread: f43e6,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,gid115aec,gidf43e6,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!g14g2000cwa.googlegroups.com!not-for-mail From: fmdf@tiscali.it Newsgroups: comp.lang.ada,comp.lang.c++,comp.realtime,comp.software-eng Subject: Re: Teaching new tricks to an old dog (C++ -->Ada) Date: 10 Mar 2005 18:46:36 -0800 Organization: http://groups.google.com Message-ID: <1110509196.414374.178490@g14g2000cwa.googlegroups.com> References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110032222.447846.167060@g14g2000cwa.googlegroups.com> <871xau9nlh.fsf@insalien.org> <3SjWd.103128$Vf.3969241@news000.worldonline.dk> <87r7iu85lf.fsf@insalien.org> <1110052142.832650@athnrd02> <1110508346.652721.170680@g14g2000cwa.googlegroups.com> NNTP-Posting-Host: 80.181.51.48 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1110509201 28245 127.0.0.1 (11 Mar 2005 02:46:41 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 11 Mar 2005 02:46:41 +0000 (UTC) In-Reply-To: <1110508346.652721.170680@g14g2000cwa.googlegroups.com> User-Agent: G2/0.2 Complaints-To: groups-abuse@google.com Injection-Info: g14g2000cwa.googlegroups.com; posting-host=80.181.51.48; posting-account=Lp02jQ0AAABMd3TAghNf0TM2YBZqD_JE Xref: g2news1.google.com comp.lang.ada:9080 comp.lang.c++:45039 comp.realtime:1229 comp.software-eng:4797 Date: 2005-03-10T18:46:36-08:00 List-Id: f...@tiscali.it wrote: > Alberto wrote: > > > 3. Many of you want us to believe that ADa performs ra nge checking > > without loss of performance: it can be true at compile time with > fixed > > ranges, but it can't definitely be done without chechinkg array > bounds > > every time data is accessed if we don't know these bounds before > > compiling (e.g.: typical cases where dynamic allocation of memory is > > used) > > Do you think C++ is more performing at range checking? When you don't > anymore need checkings in a Ada program you can use "Pragma > Suppress()". What can you do in a C++ program? I suppose you can only > rewrite that code. > > Suppose you want a class type to hold a value in range -100 .. 100, I > think you have to code a lot to build a class that can't be mistakenly > used to hold absolut greater values. > > Please compare the following two simple programs each of them have a > bug overflowing a value. I am sure that they can be better coded, or > that better solutions can be implemented as I'm not either a C++ or Ada > professional programmer, yet the C++ code is not as expressive and > compact as the Ada counterpart. > > // over.cpp > > #include > > using std::cout; > using std::ostream; > > class Range_Error > { > public: > Range_Error() > : message( "raised \"Out of Range\"" ) {} > const char* what() const { return message; } > private: > const char* message; > }; > > class Var_T > { > public: > Var_T( const int& i = int() ) > { > assign( i ); > } > Var_T& operator++( ) > { > assign( ++Var ); > return *this; > } > bool assign( const int& i ) > { > if ( i >= 0 && i <= 100 ) > Var = i; > else throw Range_Error(); > return true; > } > friend std::ostream& operator<<( std::ostream& out, const Var_T& > obj ) > { > out << obj.Var; > return out; > } > private: > int Var; > }; > > int main() > { > Var_T Var( 0 ); > try > { > for ( int i = 0; i <= 100; ++i ) // range check error expected > ++Var; > cout << Var << '\n'; > } > catch( Range_Error ERR ) > { > cout << ERR.what() << '\n'; > } > return 0; > } > > > -- over.adb > > with Ada.Text_IO; > use Ada.Text_IO; > > procedure Over is > -- Pragma Suppress( Range_Check ); > type Var_T is range 0 .. 100; > package Var_IO is new Ada.Text_IO.Integer_IO( Var_T ); > Var : Var_T := 0; > > begin > for Index in 0 .. 100 loop -- range check failing is expected > Var := Var + 1; > end loop; > Var_IO.Put ( Var ); > New_Line; > exception > when Constraint_Error => > Put_Line ( "Constrained Error" ); > when others => > Put_Line ( "Unknown Error" ); > end Over; > > > Ada example is less than half longer than C++, it is most expressive, > it catches all exceptions (not only out of range) and it suppresses all > range checks when uncommenting "Pragma". Sorry for my English... Please read: Ada example is more than a half shorter than C++ one, it is more expressive, it ... > > What dou you think of this kind of Ada unefficient and too many lines > of code example? :-) > > Ciao, > > fabio de francesco