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: fc772,b30bd69fa8f63cb2 X-Google-Attributes: gidfc772,public X-Google-Thread: 103376,b30bd69fa8f63cb2 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-06-14 09:20:27 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.ems.psu.edu!news.cse.psu.edu!uwm.edu!rpi!not-for-mail From: Ed Avis Newsgroups: comp.lang.ada,comp.lang.c++.moderated Subject: Bounded integer types (was: C bug of the day) Followup-To: comp.lang.c++.moderated Date: 14 Jun 2003 12:22:34 -0400 Organization: unknown Sender: cppmods@netlab.cs.rpi.edu Message-ID: References: <1054751321.434656@master.nyc.kbcfp.com> NNTP-Posting-Host: netlab.cs.rpi.edu X-Original-Date: 14 Jun 2003 10:47:21 +0100 X-Submission-Address: c++-submit@netlab.cs.rpi.edu X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated iQBVAwUAPutLx0HMCo9UcraBAQFuyQIAn5jRgJ2xVWKMUUBlC1WoorO0AEo+5s6/ NzGDb2I+5Ug0hENe4g9PvqPYbSATZxmhbXZBWQdjkZh948j6Gn1rOw== =NISn Xref: archiver1.google.com comp.lang.ada:39177 comp.lang.c++.moderated:68352 Date: 2003-06-14T12:22:34-04:00 List-Id: James Rogers writes: >Compare this with defining a numeric type with all integers from >1 through 101: > >type My_Ints is range 1..101; > >In this case Ada automatically provides all the checking and >numeric operators for you. FWIW, I have been working on an equivalent for C++, as bounded i; to say that i has values in the range 1..10 (I use .. to mean an inclusive range, I hope that it doesn't mean something different in Ada-land). Then operations like addition are implemented so that all range checking can be done statically - no checks at run time, except for an explicit range_cast() function which turns an int into a bounded. So for example the type of (i + i) would be bounded. If you wanted to assign this to a variable that holds only values 1..10 you would need a range_cast and a possible exception at run time. But assigning it to a variable that can take a larger range (or to a plain old int) is fine. Apart from operator=, there are no mutating operations like ++ or +=, because these cannot be statically checked. And operator= takes only another bounded or a compile-time constant. The aim of the bounded type is to have no overhead beyond an ordinary int (if the compiler is reasonably good) and to have no exceptions thrown except when the programmer asks for them with range_cast. To emphasize one of those points: you cannot assign or initialize a bounded with a value that's not known at compile time. The default value of an uninitialized bounded type is the minimum value of the range. If I wanted to declare i with range 1..10 and initialize it to 5, I'd have to write bounded i = bounded(); This is a horrible syntax but I couldn't find any much cleaner way of specifying that the constructor must take a compile-time constant. I realized this was a bit limiting so I have also started work on a throwing_bounded class which does have operator=, operator++ and friends, and throws exceptions at run time if you go outside the range. Followups set to the C++ newsgroup only. -- Ed Avis [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ] [ about comp.lang.c++.moderated. First time posters: do this! ]