comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: Question about finalization of local object returned from the function
Date: Sun, 1 Sep 2019 11:34:05 -0700 (PDT)
Date: 2019-09-01T11:34:05-07:00	[thread overview]
Message-ID: <a72343a2-1a48-42a2-b125-4b123c94d28d@googlegroups.com> (raw)
In-Reply-To: <a2264c91-67b6-4846-a415-e4428dcdfaa2@googlegroups.com>

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

  reply	other threads:[~2019-09-01 18:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-28 22:36 Question about finalization of local object returned from the function darkestkhan
2019-08-29  1:45 ` Brad Moore
2019-09-01 18:34   ` Stephen Leake [this message]
2019-09-01 22:12     ` darkestkhan
2019-09-02 17:14       ` Jeffrey R. Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox