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

  1. Path: sparky!uunet!dtix!darwin.sura.net!rsg1.er.usgs.gov!news.cs.indiana.edu!syscon!gator!fang!att!allegra!alice!ark
  2. From: ark@alice.att.com (Andrew Koenig)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: destruction of temporaries
  5. Message-ID: <23471@alice.att.com>
  6. Date: 18 Aug 92 13:51:10 GMT
  7. References: <BszApE.49v@world.std.com> <1992Aug17.073500.24115@ericsson.se> <23466@alice.att.com> <1992Aug17.211508.6634@lucid.com>
  8. Reply-To: ark@alice.UUCP ()
  9. Organization: AT&T Bell Laboratories, Liberty Corner NJ
  10. Lines: 81
  11.  
  12. In article <1992Aug17.211508.6634@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
  13.  
  14. > And I feel that I should be able to add a { } around an expression
  15. > statement without changing its meaning.  In other words I ought
  16. > to be able to further transform the above to
  17.  
  18. >         const char* r = "" ;
  19. >         if ( 1 ) { r = s+t ; } 
  20. >         printf("%s\n", r) 
  21.  
  22. > Even "end of block" (the latest anyone has proposed) makes
  23. > this example undefined.
  24.  
  25. In other words, no algorithm for destroying temporaries will give
  26. you what you want; what you want is impossible.  This argument is
  27. therefore scarcely relevant.
  28.  
  29. > ... Early destruction makes many useful idioms impossible. 
  30.  
  31. Agreed.  The real question is which of the other alternatives is
  32. the best balance between efficiency, difficulty of implementation,
  33. and so on.
  34.  
  35. > |> 
  36. > |>     2. It is a real pain to implement for statements that
  37. > |>        generate temporaries inside conditionals.  
  38.  
  39. > Yes, it requires more effort on the part of compiler vendors to get it 
  40. > right,  but a lot less than many other features of C++.  
  41.  
  42. I don't think so; at least not in the presence of exception handling.
  43. The trouble is that a naive approach can potentially make programs
  44. either big or slow very quickly and optimizing it is hard enough that
  45. compilers are likely to get it wrong in practice.
  46.  
  47. > It is possible to construct examples where the bookkeeping is a significant
  48. > overhead. But I don't believe this is a substantial problem in practice. 
  49.  
  50. That's true only if people usually don't write programs for which it
  51. makes a difference, in which case this whole discussion is moot.
  52.  
  53. > It isn't quite true that it becomes impossible to have a static map.
  54. > The cost of doing so is duplicated code.
  55.  
  56. The size of the duplicate code is potentially exponential in the size
  57. of the original program; concealing exponential code growth seems to
  58. be a nasty thing to do to a programmer.
  59.  
  60. > An alternative implementation requires flags in some cases to
  61. > determine which temps have been constructed, and the static
  62. > data structure would have to indicate this. The normal case where there
  63. > is no flag would require no extra overheads.
  64.  
  65. That's not the point.  The real problem is that now the code that
  66. unwinds exceptions has to know where the flags are and test them.
  67. That means, for example, that the flags cannot be kept in registers.
  68. It also makes exception unwinding much harder to implement.
  69.  
  70. > Compared to the effort to implement exceptions in the first place,
  71. > the extra effort required to get the lifetime of temporaries correct
  72. > doesn't seem inordinate.
  73.  
  74. > For these reasons I believe "end of statement" (or more properly
  75. > "end of outermost expression") is the best answer for lifetime
  76. > of temporaries.
  77.  
  78. Since we are trying to establish which of several possible implementation
  79. strategies is "correct," I think these last two paragraphs are a
  80. circular argument.  My counterclaim:
  81.  
  82. Destroying temporaries at end of statement offers implementors a choice
  83. between potentially exponential code growth and greatly increased
  84. difficulty of implementing exception handling.  This dilemma exists only
  85. to make it easier to handle expressions of a kind people write in practice
  86. only very rarely.
  87.  
  88. For these reasons, I believe that temporaries created in a conditional
  89. context should be destroyed in that context.
  90. -- 
  91.                 --Andrew Koenig
  92.                   ark@europa.att.com
  93.