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

  1. Path: sparky!uunet!mcsun!corton!seti!goudurix.inria.fr!edelson
  2. From: daniel.edelson@inria.fr (Daniel R. Edelson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: destruction of temporaries
  5. Message-ID: <4095@seti.UUCP>
  6. Date: 21 Aug 92 08:57:14 GMT
  7. References: <BszApE.49v@world.std.com> <1992Aug17.073500.24115@ericsson.se> <TMB.92Aug20142610@arolla.idiap.ch>
  8. Sender: news@seti.UUCP
  9. Reply-To: Daniel R. Edelson <daniel.edelson@inria.fr>
  10. Organization: INRIA -- Institut National de Recherche en Informatique et Automatique -- Rocquencourt, France
  11. Lines: 94
  12.  
  13. In article <TMB.92Aug20142610@arolla.idiap.ch>, tmb@arolla.idiap.ch (Thomas M. Breuel) writes:
  14. |> In article <23487@alice.att.com> ark@alice.att.com (Andrew Koenig) writes:
  15. |> 
  16. |>    Destruction at end of block is far from a hopeless approach, but I
  17. |>    think it loses on balance.  Indeed, there's something to be said for
  18. |>    almost every approach, which is why this is a knotty problem.
  19. |> 
  20. |> Let me put in another vote for leaving things the way they are.
  21.  
  22. Please, no. Nobody is happy with things the way they are now.
  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. It depends, maybe end of expression isn't long enough. Maybe end of
  36. block isn't even long enough.
  37.  
  38. |> The price you pay is that of making another set of programs use
  39. |> (sometimes considerably) more memory. This is important in particular
  40. |> for scientific applications, which can use many large temporaries
  41. |> during a single expression. Such expressions (usually already tricky
  42. |> the way they are) would be nearly impossible to rewrite by hand in
  43. |> order to minimize memory use.
  44.  
  45. A previous poster mentioned quality of implementation.
  46. I couldn't agree more. Scientific computation is a problem 
  47. for C++ operator overloading. Are we even certain that destroying
  48. temporaries immediately makes C++ suitable for scientific
  49. computation? At the the Usenix C++ conference advanced topics
  50. workshop, we saw a presentation by a person from Boeing (I'm sorry,
  51. my material is at home and I don't remember his name) who pointed
  52. out some of the difficulties with scientific computations. One problem
  53. is using the chaining capability of a cray.
  54.  
  55. Assume you have a matrix expression like 
  56.     D =  A * B + C;
  57. Assume further that the compiler figures out how to vectorize
  58. the multiplication. The next step is to chain the multiplication
  59. to the addition. But this is very difficult because the functions
  60. that implement the overloaded operators hide the fact that the loops
  61. chain.
  62.  
  63. The point is, scientific computing is difficult. What are the
  64. ramifications of dictating that temporaries get destroyed
  65. very quickly. Does that suffice to make C++ acceptable in
  66. numerical computing? Does that, on the other hand, make C++
  67. a more difficult language for the general user?
  68.  
  69. I think the programming language should be a general one. 
  70. There can be a lot of room for high-quality implementations
  71. and optimizations. 
  72.  
  73. |> Maybe the ANSI C++ committee will accept this change.  I think it
  74. |> would be a big mistake. I'm generally for features that make life
  75. |> easier for the programmer, but the price for this one is simply too
  76. |> high.
  77.  
  78. You may be right, but you haven't convinced me. 
  79. Suppose the committee embraces the semantics of late
  80. destruction but permits 'as-if' early destruction. That is,
  81. the compiler can destroy them early if doing so doesn't change
  82. the meaning of the program. In general, it's probably impossible
  83. to figure this out. But there may be cases in which it's possible. 
  84.  
  85. |> You can avoid unexpected behavior involving temporaries by programming
  86. |> defensively. In particular, this means: do _not_ return a reference or
  87. |> pointer to an explicit or implicit argument or its contents (*this,
  88. |> call-by-value, call-by-reference). Furthermore, do _not_ define cast
  89. |> operators to pointer types in your classes (instead, make them normal
  90. |> member functions that you have to call explicitly).
  91.  
  92. Then how do you every use smart pointers?
  93. Kiss reference counting goodbye, or else we all will be
  94. using accessors, not necessarily a bad thing.
  95.  
  96. |>                     Thomas.
  97.  
  98. In the best of all possible worlds, we would have the semantics
  99. of late destruction with the efficiency of early destruction.
  100. E.g., the compiler destroys temporaries upon return from the 
  101. enclosing function, except when it can prove that destroying them
  102. earlier does not change the semantics of the program, excepting
  103. side-effects in the destructor, which are permitted to change
  104. the semantics of the program
  105.  
  106. Daniel.Edelson@inria.fr
  107.