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 X-Received: by 2002:ac8:c86:: with SMTP id n6mr25293623qti.345.1567362846014; Sun, 01 Sep 2019 11:34:06 -0700 (PDT) X-Received: by 2002:a05:6830:1db2:: with SMTP id z18mr21938883oti.110.1567362845639; Sun, 01 Sep 2019 11:34:05 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!o24no281484qtl.0!news-out.google.com!d29ni294qtg.1!nntp.google.com!o24no281477qtl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 1 Sep 2019 11:34:05 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=76.77.182.20; posting-account=W2gdXQoAAADxIuhBWhPFjUps3wUp4RhQ NNTP-Posting-Host: 76.77.182.20 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Question about finalization of local object returned from the function From: Stephen Leake Injection-Date: Sun, 01 Sep 2019 18:34:06 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader01.eternal-september.org comp.lang.ada:57095 Date: 2019-09-01T11:34:05-07:00 List-Id: On Wednesday, August 28, 2019 at 6:45:16 PM UTC-7, Brad Moore wrote: > On Wednesday, August 28, 2019 at 4:36:38 PM UTC-6, darkestkhan wrote: > > Given a function returning controlled type: > > > > function Bar return T is > > Foo : T; > > begin > > ... > > return Foo; > > end Bar; > > > > would Foo be finalized upon end of the function? > > > > Yes Foo is finalized upon end of the function, but not before copying the > object to a temporary result object which is returned to the caller. The temporary is finalized also after being assigned to the result object. > In other words, the expected result is returned to the caller, and the Foo object on the stack is also properly finalized. > However, the compiler is free to optimize away any of these steps, as long as the net effect is preserved. To expand on Brad's comment about the extended return statement, consider: function Bar return T is begin return Foo : T do ... end return; end Bar; then 'Foo' is actually the object that the client has declared/allocated. For example: declare A : T := Bar; begin ... "Foo" is actually "A", so no temporary objects are required. I don't think Limited_Controlled is required for this to happen; ARM 6.5 (24/3) mentions "built in place", but not Limited_Controlled. Also ARM 7.6 (17.1/3) gives the conditions where "built in place" is required. -- Stephe