home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!igor!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: Destruction of temporaries
- Message-ID: <rmartin.715524432@thor>
- Date: 3 Sep 92 12:47:12 GMT
- References: <rmartin.715267769@thor> <1992Sep2.042030.488@frumious.uucp> <1992Sep2.192339.2774@lucid.com> <1992Sep2.223451.24804@tfs.com> <1992Sep3.020851.7240@lucid.com>
- Sender: news@Rational.COM
- Lines: 56
-
- Part of the following suggestion has been gleaned from the postings of
- others on this thread.
-
- There are cases where the lifetime of a temporary appears obvious:
-
- void f()
- {
- String& x = String("x");
- String& xx = x+x;
- String& xxx = X+x+x;
- }
-
- The temporary created by "String("x")" is being bound to an
- auto-variable. So the temporary should "inherit" the lifetime of the
- auto-variable. So too with the temporary created during the
- initialization of xx.
-
- However, there are two temporaries created during the initialization
- of xxx. One gets bound to the xxx variable, but the other is simply
- used as an argument to operator+. Lets call these btemps and utemps
- for "bound temporaries" and "unbound temporaries".
-
- I have been suggesting that any time a pointer or reference to a
- temporary is taken, that the compiler issue a warning. This alerts
- the programmer to the fact that a long lived alias *may* be being
- created to a short lived temporary.
-
- Many readers have (rightly) complained that this would generate far
- too many warnings. Let me now adjust my suggestion.
-
- A btemp is a temporary which is bound to a reference by
- initialization during the DECLARATION of the reference. This means
- that temporaries bound in declarations are btemps, but temporaries
- bound to references by argument passing are utemps!
-
- No warnings are issued regarding btemps; they become, in essence, auto
- variables. whose lifetime is the lifetime of the current block. So
- the first two lines of my code fragment above, would not issue a warning.
-
- Warnings are issued whenever a pointer or reference is taken of a utemp.
-
-
- This mitigates the warnings a bit, and I think it is reasonably
- straightforward for the compiler to figure out. There will still be
- lots of warnings emitted; line 3 of my function will still issue a
- warning for example.
-
- Are there any other "pruning" techniques by which we could recognize
- "safe" temporaries that don't require aliasing warnings?
-
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-