🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3063

FW: Inherent C++ problem (for comp.std.c++)

#3063
From: Eugene Lazutkin
Date: Mon, 22 Jan 1996 18:13
67 lines
3292 bytes
----------
From: 	Max Motovilov[SMTP:max@int.com]
Sent: 	Monday, January 22, 1996 8:52 AM
To: 	'Eugene Lazutkin'
Subject: 	Re: Inherent C++ problem (for comp.std.c++)

> Well, a typical optimisation here would be to construct the temporary
> inside op+ directly into the return value slot. The construction of 'c'
> would likely pass the address of 'c' as the return value slot. Thus, with
> suitable inlining, it becomes very efficient.

> And the current wording of the draft allows such optimisations --
> effectively arbitrary copy constructor invocations can be eliminated if
> either the copy or the original is never used again (as in this example).

This is exactly the kind of optimization that one would be expecting but it cannot
possibly work, at least not with the non-inline copy constructors:

//****** Complex.h

	class	Complex
	{
	............
		Complex( const Complex& );
	............
	};

//****** Complex.cpp

	int	stupid_global = 0;

	Complex::Complex( const Complex& c ) : re( c.re ), im( c.im )
	{
		stupid_global++;
	}

Now, in the body of the method

	Complex	operator + ( const Complex& a, const Complex& b )
	{
		return Complex( a.re+b.re, a.im+b.im );
	}

compiler has no way to know that copy constructor has a non-local side effect. 
Otherwise the case is exactly like the one suitable for optimization, that is, source 
object is never going to be used again. However the presence or absence of the 
optimization results in a different program behavior.

As for the (2) to (3) transition by means of reserving the slot by the caller rather
than the callee, the same concern applies. Standard doesn't require the compiler
to behave one way or the other thus the improper dependance on the optional
optimization may take place in the program. Since there is apparently no way to
prohibit side effects in copy constructors (without compromizing certain consitency
in the language) and it is unwise to impose any requirements on the way compilers
implement object return by value it should probably be explicitly said in the standard
that in certain cases invokation of the copy constructor (that is, method call!) is
not guaranted by the implementation. It would be still bad for a consistent design,
but at least unambiguous....

If the current wording is already supposed to provide for it, I suggest it Re:made more
explicit in regard to this problem.

...Max...
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <01BAE8B0.C408A920@dino.int.com>
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
From: Eugene Lazutkin <eugene@int.com>
Newsgroups: comp.std.c++
Subject: FW: Inherent C++ problem (for comp.std.c++)
Date: 22 Jan 1996 10:13:43 PST
Organization: -
Approved: austern@isolde.mti.sgi.com
Message-ID: <01BAE8B0.C408A920@dino.int.com>
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: Mon, 22 Jan 1996 10:02:39 -0600
Encoding: 65 TEXT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMQPT9Uy4NqrwXLNJAQHbywH/QpYbBmEmJxb8uOnYIBQPr0qxXo6cJxiz
 mRlKsY+wjzF0T1JgfwXEA6DCIZN/pViRLXHAW4WRavV0EG5T4A5TZQ==
 =x+7V
Originator: austern@isolde.mti.sgi.com