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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,cf3a056f36b4078f,start X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,cf3a056f36b4078f,start X-Google-Attributes: gid103376,public From: nasser@apldbio.com (Nasser Abbasi) Subject: Ada vs C++ for numeric IO (integers) Date: 1996/07/22 Message-ID: #1/1 X-Deja-AN: 169446821 sender: news@biosys.apldbio.COM original-sender: nasser@apldbio.com followup-to: comp.lang.ada organization: Applied BioSystems newsgroups: comp.lang.ada,comp.lang.c++ Date: 1996-07-22T00:00:00+00:00 List-Id: hi, I was playing with integer IO to see how C++ and Ada handle it. I noticed couple of things, I show the Ada and the C++ examples, and have a question on each language. I post this in one article just to show the difference between the 2 cases, and may be have a discussion on how each langauge approaches this area. Ada case: ======== I noticed that constraint error is not raised for short_integer if I input a value outside short_integer range in an IO operation for example. I check the LRM, and it does say that in 3.5.4.20 . But this kind'a made me think this is not too nice, as I expected an exception to be raised from Ada. Any thought on this, why does not a derived type inherit a new constraint range for its new type range? Now this means one have to figure a way to check for this case themselves instead of letting the run-time do it. for example: with Ada.Text_Io; use Ada.Text_Io; procedure Main is package Short_Io is new Ada.Text_Io.Integer_Io(Short_Integer); use Short_Io; I: Short_Integer; begin loop Put("i ? "); Get(I); Put("you typed: "); Put(I); New_line; end loop; end Main; and runnning the above program: i ? 32768 <--- I typed this expecting to get constraint error you typed: -32768 <-- it did not! , it wrapped around instead ! i ? 2147483648 <-- now, typed in value outside range of BASE type raised ADA.IO_EXCEPTIONS.DATA_ERROR <--- now we get the constraint error C++ case ======== C++ does not raise exceptions here, at least not with the compiler I am using now. Behaviour is The same with Ada for values outside range of -32768..32767, actually the same for range (-2147483647-1) ..2147483647 , i.e. no error is detected for short int IO, if one type a value larger than 32767 it wrappes around. But in C++, If I type in a value larger than 2147483647, say 2147483648, it still wrappes (moves -1 to my variable), and the cin stream is still in good state, while in Ada a constraint error is raised here. Example: #include #include main() { short int i; cin.unsetf(ios::skipws); for(;;) { cout<<"Enter i and hit return: "<>i; if( cin ) { cin.ignore(INT_MAX,'\n'); cout<