home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12633 < prev    next >
Encoding:
Internet Message Format  |  1992-08-20  |  2.4 KB

  1. Path: sparky!uunet!usc!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: destruction of temporaries
  5. Message-ID: <TMB.92Aug20142610@arolla.idiap.ch>
  6. Date: 20 Aug 92 18:26:10 GMT
  7. References: <BszApE.49v@world.std.com> <1992Aug17.073500.24115@ericsson.se>
  8.     <23466@alice.att.com> <3920@starlab.UUCP> <23487@alice.att.com>
  9. Sender: news@ai.mit.edu
  10. Reply-To: tmb@idiap.ch
  11. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  12.     Perceptive)
  13. Lines: 39
  14. In-reply-to: ark@alice.att.com's message of 19 Aug 92 18:22:44 GMT
  15.  
  16. In article <23487@alice.att.com> ark@alice.att.com (Andrew Koenig) writes:
  17.  
  18.    Destruction at end of block is far from a hopeless approach, but I
  19.    think it loses on balance.  Indeed, there's something to be said for
  20.    almost every approach, which is why this is a knotty problem.
  21.  
  22. Let me put in another vote for leaving things the way they are.
  23.  
  24. People want temporaries to live longer so that they can write
  25. certain expression that involve returning pointers or references
  26. to (the contents of) temporaries.
  27.  
  28. However, delaying the destruction of temporaries (say, to the end of
  29. an expression) fixes nothing. It makes a slightly larger set of
  30. programs acceptable, but you still have to know in detail whether some
  31. object has been derived in one way or another from a temporary. For
  32. example, "char *p = s+t;" still leaves "p" undefined (if s and t are
  33. Strings).
  34.  
  35. The price you pay is that of making another set of programs use
  36. (sometimes considerably) more memory. This is important in particular
  37. for scientific applications, which can use many large temporaries
  38. during a single expression. Such expressions (usually already tricky
  39. the way they are) would be nearly impossible to rewrite by hand in
  40. order to minimize memory use.
  41.  
  42. Maybe the ANSI C++ committee will accept this change.  I think it
  43. would be a big mistake. I'm generally for features that make life
  44. easier for the programmer, but the price for this one is simply too
  45. high.
  46.  
  47. You can avoid unexpected behavior involving temporaries by programming
  48. defensively. In particular, this means: do _not_ return a reference or
  49. pointer to an explicit or implicit argument or its contents (*this,
  50. call-by-value, call-by-reference). Furthermore, do _not_ define cast
  51. operators to pointer types in your classes (instead, make them normal
  52. member functions that you have to call explicitly).
  53.  
  54.                     Thomas.
  55.