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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,eca91508b02e7e97 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!news.cid.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Alex R. Mosteo" Newsgroups: comp.lang.ada Subject: Re: Amount of copying on returned constant objects Date: Mon, 18 Jun 2007 20:23:49 +0300 Message-ID: <5dntd6F35jc57U1@mid.individual.net> References: <5dfsfnF1qav8bU1@mid.individual.net> <46730bf5$0$23134$9b4e6d93@newsspool1.arcor-online.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: individual.net uNcd2MlrY8KNKm6BPlc0pgEZ+IjUbzlIJeGzXQlg09RhLr2Cs= User-Agent: KNode/0.10.5 Xref: g2news1.google.com comp.lang.ada:16222 Date: 2007-06-18T20:23:49+03:00 List-Id: Georg Bauhaus wrote: > Alex R. Mosteo wrote: >> >> >> I wonder however about results of functions, that are not modified. >> >> ... In C++ I could use constant references. Now, I wonder if >> >> a) is there something in the ARM that prevents an equivalent transparent >> optimization in the Ada side (returning the reference when it is detected >> that the returned object is not modified)? > > OTOH, I might want a constant copy because the object in the > container is going to be modified... Can a compiler detect this? I don't think it can, at least easily. I guess that if you're keeping a copy, constant or not, the optimization opportunity is lost. But what about short-lived objects, like... if Container.Element ("key").Is_Nice then -- Container for some tagged type ... end if; This is the kind of copies that I see interesting to optimize away. I don't know enough about compilers to say if it is reasonable to expect one to detect this situation or not. If not, one possibility would be to have function Element (Key : Key_Type) return Element_Type; and type Constant_Access is access constant Element_Type; function Element (Key : Key_Type) return Constant_Access; but I'm not sure about the amount of ambiguities one would get in that case. In any case this does not exist in the standard 05 containers.