[comp.lang.c++] references as class member variables

schmidt@crimee.ics.uci.edu (Douglas C. Schmidt) (05/30/91)

Hi,

	I'm curious...  Does anyone know what the "proper" output for
the following program should be (or is it perhaps officially undefined?)
g++ prints 10, 11, whereas cfront 2.0 prints 11, 11.

--------------------------------------
struct Foo
{
  int &r;

  Foo (int s): r (s) { }
};

extern "C" void printf (const char *, ...);

main ()
{
  Foo f (10);
  Foo g (11);

  printf ("%d, %d\n", f.r, g.r);
}
----------------------------------------

Thanks,

	Doug
--
His life was gentle, and the elements so            | Douglas C. Schmidt
Mixed in him that nature might stand up             | (schmidt@ics.uci.edu)
And say to all the world: "This was a man."         | (714) 856-4101
   -- In loving memory of Terry Williams (1971-1991)|

niklas@appli.se (Niklas Hallqvist) (06/03/91)

schmidt@crimee.ics.uci.edu (Douglas C. Schmidt) writes:

>Hi,

>	I'm curious...  Does anyone know what the "proper" output for
>the following program should be (or is it perhaps officially undefined?)
>g++ prints 10, 11, whereas cfront 2.0 prints 11, 11.

>--------------------------------------
>struct Foo
>{
>  int &r;

>  Foo (int s): r (s) { }
>};

>extern "C" void printf (const char *, ...);

>main ()
>{
>  Foo f (10);
>  Foo g (11);

>  printf ("%d, %d\n", f.r, g.r);
>}
>----------------------------------------

I don't have my ARM with me, but as I recall it, this is officially
undefined.  You are aliasing a parameter passed by value, which is
risky, at least.  If the parameter is passed in a register, the compiler
is allowed to make a temporary which is then aliased.  If it's
passed in a stack slot, the stack slot will be aliased.  This stack
slot could of course be reused when it goes out of scope.  If this is
incorrect, please flame me ;->

						Niklas
-- 
Niklas Hallqvist	Phone: +46-(0)31-40 75 00
Applitron Datasystem	Fax:   +46-(0)31-83 39 50
Molndalsvagen 95	Email: niklas@appli.se
S-412 63  GOTEBORG, Sweden     mcsun!sunic!chalmers!appli!niklas