Re: Renaming versus initialisation



On Wed, 28 Sep 2005 15:12:23 GMT, Tapio Kelloniemi wrote:

> "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx> wrote:
>>On 27 Sep 2005 20:00:56 -0400, Robert A Duff wrote:
>>
>>> Tapio Kelloniemi <invalid@xxxxxxxxxxxxxxxxxxxxxxxxx> writes:
>>>
>>>> V1 : T := F;
>>>> V2 : T renames F;
>>>>
>>>> I'd like to see an explanation of the effect of an object renaming
>>>> declaration versus variable declaration and initialisation, when the object
>>>> being renamed is a return value of a function. Many thanks for it.
>>>
>>> They are pretty similar.
>>
>>What about difference in respect to creation of new objects?
>
> Renaming creates a new object as does initialisation,

No, it creates a view.

> when not
> returning by reference (return value is copied = new object is created).

Try this with GNAT:

with Ada.Finalization;
package Foo is
type T is new Ada.Finalization.Controlled with null record;
procedure Finalize (X : in out T);
function Create return T;
end Foo;
------------------------------------
with Ada.Text_IO;
package body Foo is
procedure Finalize (X : in out T) is
begin
Ada.Text_IO.Put_Line ("Finalize");
end Finalize;
function Create return T is
begin
return (Ada.Finalization.Controlled with null record);
end Create;
end Foo;
---------------------------------
with Foo; use Foo;
with Ada.Text_IO;
procedure Baz is
begin
Ada.Text_IO.Put_Line ("Initialization:");
declare
X : T := Create;
begin
null;
end;
Ada.Text_IO.Put_Line ("Renaming:");
declare
X : T renames Create;
begin
null;
end;
end Baz;

The output should look like:

Initialization:
Finalize -- X value created before ":="
Finalize -- A local object in Foo
Finalize -- X value assigned by ":="
Renaming:
Finalize -- A local object in Foo
Finalize -- X value renamed

A compiler has right to optimize out the local object. I believe, it also
can optimize out initialization/finalization pair of the target in
declaration with initialization, so in the end it could be same, though it
isn't in GNAT.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.



Relevant Pages

  • Re: How to implement sizeof operator
    ... project i want to implemnent my own sizeof operator function (like ... Hint: macros can declare variables. ... difference between foo and foo, where the first foo was declared using ... No, at compile-time, it can be done in pure ISO C. ...
    (comp.lang.c)
  • Re: another way to shoot yourself in the foot?
    ... first extended return statement that raises an exception doesn't have ... in the sense that upon initialization you could pass ... its components to prematurely finalize themselves. ... task type Foo is ...
    (comp.lang.ada)
  • Re: Subroutines with &
    ... print foo(); ... In addition to the expected "Howdy", it produces the following warning: ... You could declare the sub above the ... Or, you could forward declare the sub, like this: ...
    (comp.lang.perl.misc)
  • Re: Calling functions declared in an entity
    ... In VHDL it is possible to declare the following entity: ... entity Foo is ... ARCHITECTURE rtl OF ent_item_decl IS ...
    (comp.lang.vhdl)
  • Re: Function declarations & use of static functions
    ... > mainand foo(), respectively defined in main.c and foo.c. ... > declare foo() within main.c so maincould call it? ... int main ... dsk_readSectorin disk.h for external consumers. ...
    (comp.lang.c)