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!newsfeed.stanford.edu!logbridge.uoregon.edu!tethys.csu.net!newshub.sdsu.edu!elnk-nf2-pas!elnk-pas-nf1!newsfeed.earthlink.net!cyclone.socal.rr.com!cyclone2.kc.rr.com!news2.kc.rr.com!twister.socal.rr.com.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada Subject: Re: A question re meaning/use of the "for ... use ..." References: <41b3291e$0$44072$5fc3050@dreader2.news.tiscali.nl> From: Keith Thompson Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:pgHgAc8SZuv0OdTMVCjZRjodMrY= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Fri, 10 Dec 2004 02:26:09 GMT NNTP-Posting-Host: 66.91.240.168 X-Complaints-To: abuse@rr.com X-Trace: twister.socal.rr.com 1102645569 66.91.240.168 (Thu, 09 Dec 2004 18:26:09 PST) NNTP-Posting-Date: Thu, 09 Dec 2004 18:26:09 PST Organization: RoadRunner - West Xref: g2news1.google.com comp.lang.ada:6875 Date: 2004-12-10T02:26:09+00:00 List-Id: "Randy Brukardt" writes: > "Keith Thompson" wrote in message > news:lnmzwoh01r.fsf@nuthaus.mib.org... [...] >> 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. I presume your "type Enum_Rep_Subtype" should be "type Enum_rep". Here's an example where it doesn't work: with Ada.Unchecked_Conversion; with Ada.Text_IO; use Ada.Text_IO; procedure Foo is type Enum is (Zeros, Ones); for Enum use (Zeros => 0, Ones => 255); type Enum_Rep 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); begin Put_Line("To_Rep(Zeros) = " & Enum_Rep'Image(To_Rep(Zeros))); Put_Line("To_Rep(Ones) = " & Enum_Rep'Image(To_Rep(Ones))); end Foo; The output (GNAT, gcc 3.3.3, x86 Linux) is: To_Rep(Zeros) = 0 To_Rep(Ones) = -1 If I change the "Ones => 255" to "Ones => 127", I get the correct output, but GNAT warns: foo.adb:10:05: warning: types for unchecked conversion have different sizes foo.adb:11:05: warning: types for unchecked conversion have different sizes -- Keith Thompson (The_Other_Keith) kst-u@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this.