[comp.lang.c++] Lifetimes for Temporaries

turner@sdti.com (Prescott K. Turner) (10/26/89)

In article <DL.89Oct24075027@g.g.oswego.edu> dl@oswego.edu (Doug Lea) writes:
> ...
> 2) Change the C++ rules about lifetimes for temporaries, so that they,
> like `normal' variables have lifetimes to the end of the enclosing
> scope. This solution has merit on other grounds as well, but also
> creates some of its own difficulties. Actually, this may be going too
> far.  The lifetime rules for temporaries say that if a *reference* to
> a temp (or any part thereof?) is taken (or any ref-returning member
> function is called?), then its lifetime *is* to the end of the enclosing
> scope. The char* conversion *behaves* like a reference, but is not
> one. I once proposed that C++ allow the idiom of a char[]& to mean a
> reference to a character array. Support of this would solve this (and
> other) problems, since one could create a
>     char[]& String::chars() { return s /* or whatever */ ; }, 
> call it inside the ostream << int via `return put(dec(i).chars())',
> and everything would work just right.  But no one has ever told me
> that they particularly like this idea.

Re-reading the cfront 2.0 reference manual, I doubt the passage on the
lifetime of temporaries was written with ref-returning functions in mind.
The above code returns an expression that requires a temporary from a
ref-returning function.  I never thought that was supported by C++.
Cfront 1.2 certainly didn't implement it.

--
Prescott K. Turner, Jr.
Software Development Technologies, Inc.
P.O. Box 366, Sudbury, MA 01776 USA        (508) 443-5779
UUCP: ...{harvard,mit-eddie}!sdti!turner    Internet: turner@sdti.sdti.com