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=3.8 required=5.0 tests=BAYES_00,INVALID_MSGID, RATWARE_MS_HASH,RATWARE_OUTLOOK_NONAME autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c1071f2bf741de0,start X-Google-Attributes: gid103376,public From: "Jonas Nygren" Subject: Re: [Q]: Unchecked conversion Date: 1996/11/10 Message-ID: <01bbcf21$4b32a020$829d6482@joy.ericsson.se>#1/1 X-Deja-AN: 195644984 organization: Ericsson newsgroups: comp.lang.ada Date: 1996-11-10T00:00:00+00:00 List-Id: I asked a question on how to or if possible to convert between two array types using Unchecked_Conversion with the formal arguments to the instantiation of the generic being two unconstrained array types. In an email from Frank Petranka I got a solution that works with Gnat (3.04a at least). It doesn't use Unchecked_Conversion but a rather more direct approach with "for Tmp use at Arg'Address;", actually more convenient than Unchecked_Conversion (almost like C ;-). If you are intrested please see Franks mail below. I have verified that it works with Gnat. Are there any problems with this approach from a portability aspect? /jonas On 9 Nov 1996, Jonas Nygren wrote: > > I have defined an IO buffer type which I intend to use for my own IO > packages. > The buffer type is defined as follows: > > subtype Byte is Integer range 0..255; > > type Buffer is array (Positive range <>) of Byte; > Pragma Pack(Buffer); > for Buffer'Component_Size use 8; > > I also want to be able to convert between Buffer and String types. I use > Gnat and > I know that the underlying data representation of Buffer and String are the > same > so I thought I could do an Unchecked_Conversion: > > function To_Buffer (S : String) return Buffer is > function Convert is > new Ada.Unchecked_Conversion(String, Buffer); > begin > return Convert(S); > end To_Buffer; > I'm not sure if its portable, but I have used something similar to function To_Buffer (S : String) return Buffer is Temp_Buffer : Buffer(1..S'Length) for Temp_Buffer use at S'Address; begin return Temp_Buffer; end To_Buffer; I don't have access to an Ada compiler right now so I'm not sure that is exactly correct (may need declare or S(S'First)'Address) but this should put you on the right track. We also used unchecked conversion of pointers to these objects but I am not sure these are portable. > but Gnat gives me an error: > > unconstrained type "Standard.String" not allowed in unchecked conversion > > According to the RM95 Unchecked_Conversion should accept unconstrained > types > but implementations are allowed to impose limitations. > > My questions are: > > 1) Is this a Gnat(3.04a) imposed limitation on Unchecked_Conversion > or is my code illegal irregardless of compiler? > > 2) Is there a way of doing this type conversion, save copying? > > /jonas > > Disclaimer: The views or opinions expressed in this article are of the user and do not, in any manner, reflect that of the Navy. Frank J. Petranka Naval Surface Warfare Center (540)653-4849 Dahlgren, Va. 22448 fpetran@relay.nswc.navy.mil