Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!microsoft!jimad
From: jimad@microsoft.UUCP (Jim ADCOCK)
Newsgroups: comp.lang.c++
Subject: Re: operator []: ref vs value
Message-ID: <52037@microsoft.UUCP>
Date: 5 Mar 90 18:05:12 GMT
References: <5216@brazos.Rice.edu> <25E5841D.5B4A@tct.uucp>
Reply-To: jimad@microsoft.UUCP (Jim ADCOCK)
Organization: Microsoft Corp., Redmond WA
Lines: 27
In article dl@oswego.edu writes:
XConsider what happens in
X
Xvoid inc(float& f) { f += 1.0; }
X
Xmain()
X{
X // ...
X inc(v[0]);
X}
X
XHere, v[0] returns a reference_to_missing_array_element, which is
X`coerced' to return a constant value (0.0). The 0.0 is made into a
Xtemporary variable in order to make a reference. This temporary is
Xthen modified in inc(), without touching the underlying representation
Xin v. This is probably not what a user would have in mind.
The very latest specs on the _language_ call for a very different behavior
in this regard. Namely only if the reference is to a const is a temporary
object created. This prevents the situation mentioned where a hidden
temporary is created, modified, and then ignored -- all done behind the
users back. Instead, this scenerio will give an error. Naturally, it
will take an indeterminate amount of time for these language changes to
filter down to a particular compiler release.
Still, if one is designing a fundamental type like a sparse vector, it
would be nice if it works like a standard "C" vector.