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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.am4!peer.am4.highwinds-media.com!peer01.fr7!futter-mich.highwinds-media.com!news.highwinds-media.com!fx21.fr7.POSTED!not-for-mail Subject: Re: Need a way to convert a constant to a variable Newsgroups: comp.lang.ada References: From: Per Sandberg User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Message-ID: X-Complaints-To: abuse@usenet.se NNTP-Posting-Date: Sat, 05 Aug 2017 17:59:19 UTC Organization: usenet.se Date: Sat, 5 Aug 2017 19:59:17 +0200 X-Received-Bytes: 3505 X-Received-Body-CRC: 2463742800 X-Original-Bytes: 3454 Xref: news.eternal-september.org comp.lang.ada:47617 Date: 2017-08-05T19:59:17+02:00 List-Id: ???? A constant shall never ever change its value since the compiler is free to use the value in compile time to optimize the code if possible. So the whole suggestion is screaming "I want to do a very bad design". /P Den 2017-08-05 kl. 15:41, skrev Victor Porton: > I've sent the following email to ada-comment mailing list. I duplicate it > here. > > !topic Need a way to convert a constant to a variable > !reference Ada 2012 RM > !from Victor Porton 17-08-05 > !keywords constant, variable, view, conversion > !discussion > > Sometimes one needs to convert a constant view into variable view (I am > fully conscious that after this the programmer should take care not to > change the object of the view). > > In the following (not compilable with GNAT 7.1.0) code I present my > best attempt to solve the following problem: > > Write a function with an "in" indefinite holder with a string, return > chars_ptr corresponding to the string. > > It looks like there is no solution of this in Ada 2012 :-( > > with Interfaces.C; use Interfaces.C; > with Interfaces.C.Strings; use Interfaces.C.Strings; > with Ada.Containers.Indefinite_Holders; > > procedure Conv is > > package Char_Array_Holders is > new Ada.Containers.Indefinite_Holders(char_array); > > type C_String_Holder is new Char_Array_Holders.Holder > with null record; > > function To_C_String (Object: C_String_Holder) return chars_ptr is > Value: char_array renames Constant_Reference(Object).Element.all; > Value2: aliased Char_Array(Value'Range) with Import; > for Value2'Address use Value'Address; > begin > return To_Chars_Ptr(Char_Array_Access'(Value2'Access)); > end; > begin > null; > end; > > $ gnatgcc -c conv.adb -c conv.adb > conv.adb:13:07: warning: aliased object has explicit bounds > conv.adb:13:07: warning: declare without bounds (and with explicit > initialization) > conv.adb:13:07: warning: for use with unconstrained access > conv.adb:16:46: object subtype must statically match designated subtype > > My current workaround is to define my own "indefinite holder" type to > use it in my software instead of Ada.Containers.Indefinite_Holders. > > I propose the following language change: > > Please add 'Unchecked_Variable attribute. > > There are two possible meanings (of which we should choose one) of the > attribute: > > 1. C'Unchecked_Variable returns a variable view of a constant C. > > 2. A'Unchecked_Variable returns the corresponding access to a variable > for an access A to constant. > > Both variants seem to solve the trouble. > > Can any problem with different representation of constant and variables > appear? >