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,HEADER_SPAM autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b30bd69fa8f63cb2 X-Google-Attributes: gid103376,public X-Google-Thread: fc772,b30bd69fa8f63cb2 X-Google-Attributes: gidfc772,public X-Google-ArrivalTime: 2003-06-13 12:08:19 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!out.nntp.be!propagator2-sterling!news-in-sterling.newsfeed.com!news.moat.net!news-out.newsfeeds.com!propagator2-maxim!news-in-maxim.spamkiller.net!usc.edu!rpi!not-for-mail From: =?Windows-1252?Q?Terje_Sletteb=F8?= Newsgroups: comp.lang.ada,comp.lang.c++.moderated Subject: Re: C bug of the day Date: 13 Jun 2003 15:10:32 -0400 Organization: unknown Sender: cppmods@netlab.cs.rpi.edu Message-ID: References: <1054751321.434656@master.nyc.kbcfp.com> <3ee8901a@andromeda.datanet.hu> NNTP-Posting-Host: netlab.cs.rpi.edu X-Original-Date: Fri, 13 Jun 2003 13:57:57 +0200 X-Submission-Address: c++-submit@netlab.cs.rpi.edu X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated iQBVAwUAPuohpEHMCo9UcraBAQHFTQH/c4pHDiVvku9/2G+vvY6tCAYzxcaXD/K8 cs+5Rva8/1HhzWVmV9SzBYOX2FUk4fiJVJqDtBdvYxEeEkby5EgW3w== =QVau Xref: archiver1.google.com comp.lang.ada:39139 comp.lang.c++.moderated:68304 Date: 2003-06-13T15:10:32-04:00 List-Id: "James Rogers" wrote in message news:Xns9398C378C97C4jimmaureenrogers@204.127.36.1... > "Balog Pal" wrote in news:3ee8901a@andromeda.datanet.hu: > > > "James Rogers" wrote in message > > news:Xns9397C17B49692jimmaureenrogers@204.127.36.1... > > > What C++ allows, and provides for "free" with the STL, is the > ability to wrap the C style primitives such as arrays in a class, > with all the bounds checking built into the class. There's no mandated bounds checking in the standard library (except where explicitly noted such as std::string's at() function), but an implementation might provide it, anyway, especially in debug builds. > This provides safety at the expense of some efficiency. Although > the STL is part of the language definition, it is not part of the > language syntax. A C++ compiler cannot optimize out bounds checks > from STL or other container classes. It might, if it's able to see that the values are always within the bounds. > Ada goes a bit further in this area than most C++ programs. > Ada allows you to define your own numeric types (as primitives, not > classes). You are allowed to define several characteristics about > these types. > > type My_Index is range 1..10; > > The above line defines an integer type with a valid range of values > from 1 through 10 inclusive. Now, let's combine that definition with > an array type definition: > > type Nums is array (My_Index) of Float; > > This defines an array type indexed by the type My_Index. Each element > of the array is a Float. > > The compiler can now optimize out bounds checking for accessing > an array of Nums. The only index type allowed is type My_Index. > Every possible value of that type maps to an array element. You could also do this in C++, by creating such range types. In Ada, like in C++, there would likely be bounds checking at a different place, instead, though: in the range class, itself, ensuring that operations on it doesn't result in values outside the range. So you haven't really removed the range checking, just moved it. The array doesn't need to check, but the range class does. > Trying to assign an out of range value to a variable of My_Index > causes the program to raise the exception Constraint_Error at run time. > > Note that it would be illegal to initialize a variable of My_Index to > 0. 0 is not a valid value for My_Index. This is what I meant. Regards, Terje [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ] [ about comp.lang.c++.moderated. First time posters: do this! ]