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 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!news3.google.com!news.glorb.com!blackbush.cw.net!cw.net!newsfeed01.sul.t-online.de!t-online.de!nella.toplink-plannet.de!feed.news.toplink-plannet.de!news.tu-darmstadt.de!tsicnews.teliasonera.com!news.otenet.gr!news.grnet.gr!newsfd02.forthnet.gr!not-for-mail From: Ioannis Vranos Newsgroups: comp.lang.ada,comp.lang.c++,comp.realtime,comp.software-eng Subject: Re: Teaching new tricks to an old dog (C++ -->Ada) Date: Fri, 11 Mar 2005 06:06:55 +0200 Organization: FORTHnet S.A., Atthidon 4, GR-17671 Kalithea, Greece, Tel: +30 2109559000, Fax: +30 2109559333, url: http://www.forthnet.gr Message-ID: <1110514017.501621@athnrd02> 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: athnrd02.forthnet.gr Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: athprx02.forthnet.gr 1110514017 17880 193.92.150.73 (11 Mar 2005 04:06:57 GMT) X-Complaints-To: abuse@forthnet.gr NNTP-Posting-Date: Fri, 11 Mar 2005 04:06:57 +0000 (UTC) User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en In-Reply-To: <1110508346.652721.170680@g14g2000cwa.googlegroups.com> Cache-Post-Path: newsfd02!unknown@ppp16-adsl-170.ath.forthnet.gr Xref: g2news1.google.com comp.lang.ada:9087 comp.lang.c++:45052 comp.realtime:1234 comp.software-eng:4802 Date: 2005-03-11T06:06:55+02:00 List-Id: fmdf@tiscali.it wrote: > -- 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, Your C++ code in a bit better shape (and more spaces, I like to add spaces to make the code more readable): #include #include class Var_T { public: Var_T( const int& i = int() ) { if ( i >= 0 && i <= 100 ) Var = i; else throw std::out_of_range("raised \"Out of Range\""); } Var_T& operator++( ) { assign( ++Var ); return *this; } bool assign( const int& i ) { if ( i >= 0 && i <= 100 ) Var = i; else throw std::out_of_range("raised \"Out of Range\""); return true; } int rvalue() const { return Var; } private: int Var; }; inline std::ostream& operator<<( std::ostream &out, const Var_T &obj ) { out << obj.rvalue(); return out; } int main() try { Var_T Var; for ( int i = 0; i <= 100; ++i ) // range check error expected ++Var; std::cout << Var << "\n"; } catch(std::exception &ERR) { std::cerr << ERR.what() << "\n"; } > it catches all exceptions (not only out of range) and it suppresses all > range checks when uncommenting "Pragma". > > What dou you think of this kind of Ada unefficient and too many lines > of code example? At first personally I do not think Ada is inefficient, but I have the feeling that C++ is more powerful. About the run-time checks one can easily implement a macro mechanism to disable checks too, with the definition of a constant like before the compilation like: #define NOCHECKS Second, templates can be used for compile-time constraints, but I do not know these stuff yet, so perhaps someone may provide this. And then one could provide such a checking mechanism via templates (not of course to mention once again template metaprogramming where an increment operation could become compile-time). -- Ioannis Vranos http://www23.brinkster.com/noicys