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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!sunic!ericom!falcon!jonas
  3. From: jonas@beppe.ericsson.se (Jonas Nygren)
  4. Subject: Re: destruction of temporaries
  5. Message-ID: <1992Aug18.071116.3971@ericsson.se>
  6. Sender: news@ericsson.se
  7. Nntp-Posting-Host: falcon.ericsson.se
  8. Reply-To: jonas@beppe.ericsson.se
  9. Organization: Ericsson Telecom AB
  10. References: <23466@alice.att.com>
  11. Date: Tue, 18 Aug 1992 07:11:16 GMT
  12. Lines: 67
  13.  
  14. In article 23466@alice.att.com, ark@alice.att.com (Andrew Koenig) writes:
  15. > In article <1992Aug17.073500.24115@ericsson.se> jonas@beppe.ericsson.se writes:
  16. > > IMO temporaries should survive until end of statement.
  17. > This treatment has two significant disadvantages:
  18. >     1. If I break a statement up into several, it may
  19. >        quietly change its meaning.  Thus, for example, if
  20. >        String::operator const char*() exists, and s and t are
  21. >        of class String, then
  22. >         printf("%s\n", (const char*)(s+t));
  23. >     
  24. >        might work but
  25. >         const char* r = s + t;
  26. >         printf("%s\n", r);
  27. >        
  28. >        might not.
  29.  
  30. Here the programmer must be aware that a conversion takes place and should take
  31. necessary actions. Though, I agree it isn't obvious until you have had this bug.
  32.  
  33. >     2. It is a real pain to implement for statements that
  34. >        generate temporaries inside conditionals.  Thus, for
  35. >        example, in x>0?f(p+q):r, if p+q generates a temporary,
  36. >        and it was necessary to wait until the end of the statement
  37. >        to destroy it, it would also be necessary to remember which
  38. >        branch of the ?: was taken to decide whether or not to
  39. >        destroy it.  There are cases where this can add substantially
  40. >        to execution time.  Moreover, it greately complicates the
  41. >        implementation of exception handling, because it makes it
  42. >        impossible to have a static map from program counter value
  43. >        to offsets of statics to destroy.
  44.  
  45. I get very suspicious when I here about how difficult things are to do in a compiler, e.g. 
  46. I can't accept that extra space in templates x<y<int> > which is claimed to be so difficult to solve. 
  47. (I have never received so many replies as to the article where I claimed this to be a bug).
  48.  
  49. Without any compiler experience I solved this in gcc with one days work. Think of all irritation
  50. it causes to forget this little space, and how relatively simple it can be alleviated.
  51.  
  52. > For these reasons, I think a shorter lifetime than end of statement
  53. > is appropriate for temporaries.  Whether the committee will go that
  54. > way, however, remains to be seen.
  55.  
  56. Perhaps end of statement is not the correct point for destruction of temporaries but I hope you agree
  57. that the small example I showed where:
  58.  
  59.     s2 = s1.sub(2,3);
  60.  
  61. works, but not
  62.  
  63.     s2 = s1.sub(2,3).strip();
  64.  
  65. is a bit to subtle to be acceptable. 
  66.  
  67. > -- 
  68. >                 --Andrew Koenig
  69. >                   ark@europa.att.com
  70.  
  71.  
  72. /jonas
  73.  
  74.