Subject: Re: standards/5959: c++ language specification error or compiler
To: Todd Vierling <tv@pobox.com>
From: Martin Husemann <martin@rumolt.teuto.de>
List: netbsd-bugs
Date: 08/13/1998 11:11:27
> : will yield "4" as the output, in effect, assigning to an rvalue.  and
> : int is certainly not a c++ class.
> 
> The compiler is being significantly upgraded in NetBSD-current this upcoming
> weekend.  I don't have access to the machine with my egcs compilers right
> now, but it is my feeling that this does not work on the newer gcc (egcs)
> that will be added.

Two points:

with "gcc version egcs-2.90.27 980315 (egcs-1.0.2 release)" it compiles
and outputs "4".

I think this is correct, and checked the same code with Microsoft Visual C++ 5.0,
which printed "4" as well.

Let's look at the code:

#include <stdio.h>
	void function(int &t) { t=4; }
int main(int, char**)
{
	int foo;
	function(foo=3);
	printf("%d\n",foo);
	return 0;
}

What happens is:

(a) you assign 3 to variable "foo"
(b) you pass a reference to "foo" to "function"
(c) the referenced variable is set to 4 inside "function".

You could rewrite the code equivalently as:

	foo=3;
	function(foo);

So nowhere you assign something to an rvalue.

Parameters are evaluated before calling the function. This is true also for
parameters with side effects.


Disclaimer: I'm no standard's guru, and I wouldn't write code like this ;-)


Martin