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,227757d168eaa8a5 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local1.nntp.dca.giganews.com!nntp.megapath.net!news.megapath.net.POSTED!not-for-mail NNTP-Posting-Date: Thu, 09 Dec 2004 17:04:43 -0600 From: "Randy Brukardt" Newsgroups: comp.lang.ada References: <41b3291e$0$44072$5fc3050@dreader2.news.tiscali.nl> Subject: Re: A question re meaning/use of the "for ... use ..." Date: Thu, 9 Dec 2004 17:06:01 -0600 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 Message-ID: NNTP-Posting-Host: 64.32.209.38 X-Trace: sv3-loTmLP2E4n+T5U+ucdwTfqUwR3idUUgvripsFECfwu5LwDKN1jjbqI0HfxgEF6OCtq9BKN44br+ffZ8!WO63Mxa6Z1k5+ZzyA+nXunwVEze6VZ8Dt+2JUN9840ln+FJWBe2XtZxTjtRAVAYEMgL3FcCESEEe X-Complaints-To: abuse@megapath.net X-DMCA-Complaints-To: abuse@megapath.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.20 Xref: g2news1.google.com comp.lang.ada:6873 Date: 2004-12-09T17:06:01-06:00 List-Id: "Keith Thompson" wrote in message news:lnmzwoh01r.fsf@nuthaus.mib.org... > "Randy Brukardt" writes: > > "Keith Thompson" wrote in message > > news:ln8y8932ei.fsf@nuthaus.mib.org... > [...] > >> How do you portably choose the target type for the Unchecked_Conversion? > >> > >> The 'Pos attribute returns a result of type universal_integer; there's > >> no way to make an Unchecked_Conversion return a universal_integer. > > > > You have to declare a type for that purpose, but otherwise there is no > > problem: > > > > First_Rep : constant := ; > > Last_Rep : constant := ; > > type Enum is (First, ...., Last); > > for Enum use (First => First_Rep, .... Last => Last_Rep); > > type Enum_Rep is range First_Rep .. Last_Rep; > > for Enum_Rep'Size use Enum'Size; > > function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep); > > function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep, Enum); > > > > All of this is portable, and required for any Annex C compliant compiler. > > The only loss here is writing a bit of extra text (these declarations, and > > type conversions on the results of the functions). This is a rare enough > > need that that doesn't seem too bad. > > That's fine if the author of the code that declares the type Enum has > also bothered to declare the type Enum_Rep (and has done so > correctly). > > But suppose I'm using some third-party package that declares an > enumeration type. I have no control over the coding of the package. > The type may or may not have a representation clause; if it doesn't, > it may in the next version. As a client of the package, I don't have > enough information to declare the type Enum_Rep myself. Baloney. All you need is the 'Size value. type Enum_Rep_Subtype is range 0 .. 1; for Enum_Rep'Size use Enum'Size; function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep'Base); function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep'Base, Enum); You do lose any range checking, but that's relatively unimportant. > It would also be nice to have an operation like From_Rep that raises > Constraint_Error if I call it with an invalid value. You've got 'Valid for that purpose. The ARG has made it clear that a program that uses Unchecked_Conversion to create a value that is immediately checked with 'Valid is not erroneous, no matter what the value is. (The RM is not clear on this point.) And certainly you need to check for problems if there is a possibility of one. Randy. P.S. Thanks for reminding me about 'Valid; I need to use it to fix a bug that I ran across yesterday, and I'd forgotten how...