home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12444 < prev    next >
Encoding:
Text File  |  1992-08-17  |  3.6 KB  |  112 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!lucid.com!lucid.com!jss
  3. From: jss@lucid.com (Jerry Schwarz)
  4. Subject: Re: destruction of temporaries
  5. Message-ID: <1992Aug17.211508.6634@lucid.com>
  6. Sender: usenet@lucid.com
  7. Reply-To: jss@lucid.com (Jerry Schwarz)
  8. Organization: Lucid, Inc.
  9. References: <BszApE.49v@world.std.com> <1992Aug17.073500.24115@ericsson.se> <23466@alice.att.com>
  10. Date: Mon, 17 Aug 92 21:15:08 GMT
  11. Lines: 99
  12.  
  13. In article <23466@alice.att.com>, ark@alice.att.com (Andrew Koenig) writes:
  14. |> This treatment [end of statement] has two significant disadvantages:
  15. |> 
  16. |>     1. If I break a statement up into several, it may
  17. |>        quietly change its meaning.  Thus, for example, if
  18. |>        String::operator const char*() exists, and s and t are
  19. |>        of class String, then
  20. |> 
  21. |>         printf("%s\n", (const char*)(s+t));
  22. |>     
  23. |>        might work but
  24. |> 
  25. |>         const char* r = s + t;
  26. |>         printf("%s\n", r);
  27. |>        
  28. |>        might not.
  29.  
  30. And I feel that I should be able to add a { } around an expression
  31. statement without changing its meaning.  In other words I ought
  32. to be able to further transform the above to
  33.  
  34.         const char* r = "" ;
  35.         if ( 1 ) { r = s+t ; } 
  36.         printf("%s\n", r) 
  37.  
  38.  
  39. Even "end of block" (the latest anyone has proposed) makes
  40. this example undefined.
  41.  
  42. Absent garbage collection, the existence of temporaries and member
  43. functions that return pointers to their internals cannot be ignored
  44. by programmers. While it is unfortunate that breaking up of a statement
  45. in an ill choosen way can have consequences.  It seems even more
  46. unfortunate to me if I can't write the original printf.  Note that 
  47. although early destruction of temporaries makes the first example undefined,
  48. it still might appear to work on many implementations. 
  49.  
  50. Early destruction makes many useful idioms impossible. 
  51. The above example is inherently suspect because it uses 
  52. member function returning a pointer to its internals.  But consider
  53.  
  54.     class Dialogbox : public istream { ... } ;
  55.     Dialogbox("enter address(in hex)") >> hex >> n ;
  56.  
  57. There is nothing suspect here unless you consider it suspicious
  58. to pass a temporary to a function expecting a reference
  59.  
  60. |> 
  61. |>     2. It is a real pain to implement for statements that
  62. |>        generate temporaries inside conditionals.  
  63.  
  64.  
  65. Yes, it requires more effort on the part of compiler vendors to get it 
  66. right,  but a lot less than many other features of C++.  
  67.  
  68.        ...
  69. |>      .  There are cases where this can add substantially
  70. |>        to execution time.
  71.  
  72. It is possible to construct examples where the bookkeeping is a significant
  73. overhead. But I don't believe this is a substantial problem in practice. 
  74.  
  75. |>         Moreover, it greately complicates the
  76. |>        implementation of exception handling, because it makes it
  77. |>        impossible to have a static map from program counter value
  78. |>        to offsets of statics to destroy.
  79.  
  80. It isn't quite true that it becomes impossible to have a static map.
  81. The cost of doing so is duplicated code.  For example
  82.  
  83.     fred(x>0?f(p+q):r) 
  84.  
  85. can to be transformed to (approximately) 
  86.  
  87.     if ( x>0 ) { fred(f(p+q)) ;
  88.     else fred(r) ;
  89.  
  90. An alternative implementation requires flags in some cases to
  91. determine which temps have been constructed, and the static
  92. data structure would have to indicate this. The normal case where there
  93. is no flag would require no extra overheads.
  94.  
  95. Compared to the effort to implement exceptions in the first place,
  96. the extra effort required to get the lifetime of temporaries correct
  97. doesn't seem inordinate.
  98.  
  99.   -- Jerry Schwarz
  100.  
  101.  
  102.  
  103.  
  104. For these reasons I believe "end of statement" (or more properly
  105. "end of outermost expression") is the best answer for lifetime
  106. of temporaries.
  107.  
  108.    -- Jerry Schwarz
  109.  
  110.  
  111.  
  112.