home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13253 < prev    next >
Encoding:
Internet Message Format  |  1992-09-03  |  2.6 KB

  1. Path: sparky!uunet!igor!thor!rmartin
  2. From: rmartin@thor.Rational.COM (Bob Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Destruction of temporaries
  5. Message-ID: <rmartin.715524432@thor>
  6. Date: 3 Sep 92 12:47:12 GMT
  7. References: <rmartin.715267769@thor> <1992Sep2.042030.488@frumious.uucp> <1992Sep2.192339.2774@lucid.com> <1992Sep2.223451.24804@tfs.com> <1992Sep3.020851.7240@lucid.com>
  8. Sender: news@Rational.COM
  9. Lines: 56
  10.  
  11. Part of the following suggestion has been gleaned from the postings of
  12. others on this thread.  
  13.  
  14. There are cases where the lifetime of a temporary appears obvious:
  15.  
  16. void f()
  17. {
  18.   String& x = String("x");
  19.   String& xx = x+x;
  20.   String& xxx = X+x+x;
  21. }
  22.  
  23. The temporary created by "String("x")" is being bound to an
  24. auto-variable.  So the temporary should "inherit" the lifetime of the
  25. auto-variable.   So too with the temporary created during the
  26. initialization of xx.
  27.  
  28. However, there are two temporaries created during the initialization
  29. of xxx.  One gets bound to the xxx variable, but the other is simply
  30. used as an argument to operator+.  Lets call these btemps and utemps
  31. for "bound temporaries" and "unbound temporaries".
  32.  
  33. I have been suggesting that any time a pointer or reference to a
  34. temporary is taken, that the compiler issue a warning.  This alerts
  35. the programmer to the fact that a long lived alias *may* be being
  36. created to a short lived temporary.  
  37.  
  38. Many readers have (rightly) complained that this would generate far
  39. too many warnings.  Let me now adjust my suggestion.
  40.  
  41. A btemp is a temporary which is bound to a reference by
  42. initialization during the DECLARATION of the reference.  This means
  43. that temporaries bound in declarations are btemps, but temporaries
  44. bound to references by argument passing are utemps!
  45.  
  46. No warnings are issued regarding btemps; they become, in essence, auto
  47. variables. whose lifetime is the lifetime of the current block.  So
  48. the first two lines of my code fragment above, would not issue a warning.
  49.  
  50. Warnings are issued whenever a pointer or reference is taken of a utemp.
  51.  
  52.  
  53. This mitigates the warnings a bit, and I think it is reasonably
  54. straightforward for the compiler to figure out.  There will still be
  55. lots of warnings emitted; line 3 of my function will still issue a
  56. warning for example.
  57.  
  58. Are there any other "pruning" techniques by which we could recognize
  59. "safe" temporaries that don't require aliasing warnings?
  60.  
  61.  
  62. --
  63. Robert Martin                        Training courses offered in:
  64. R. C. M. Consulting                       Object Oriented Analysis
  65. 2080 Cranbrook Rd.                        Object Oriented Design
  66. Green Oaks, Il 60048 (708) 918-1004       C++
  67.