home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12408 < prev    next >
Encoding:
Text File  |  1992-08-16  |  2.1 KB  |  66 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: <1992Aug17.073500.24115@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: <BszApE.49v@world.std.com>
  11. Date: Mon, 17 Aug 1992 07:35:00 GMT
  12. Lines: 52
  13.  
  14.  
  15. In article 49v@world.std.com, pkturner@world.std.com (Prescott K Turner) writes:
  16. > Bill Raves writes:
  17. > > For example, Jim Adcock has suggested
  18. > > (<70784@microsoft.UUCP>) "NEVER rely on unnamed temporaries to do
  19. > > anything...", but this would seem to erode the expressiveness of the
  20. > > language: constructs like "foo(x + y)" and "foo()[i]" would no longer
  21. > > be permissible. 
  22. > I second Adcock's advice.  Actually, those constructs can be made to
  23. > work at the expense of some extra implementation effort, and some
  24. > execution cost.  But the cost is not an impediment if it's being compared
  25. > to code which is not portable to the platforms one needs.
  26. > What the programmer must beware (in the absence of garbage 
  27. > collection) is pointers and references that refer to unnamed objects.
  28. > The standards committee may eventually mandate that these objects 
  29. > hang around somewhat longer, but not indefinitely.
  30. > Classes which rely on such pointers entail pitfalls.  Late destruction
  31. > of temporaries (as in cfront) reduces but does not eliminate
  32. > the problem.
  33.  
  34. IMO temporaries should survive until end of statement. At least they should not be destroyed
  35. as early as in gcc-2.x. In C you can write:
  36.  
  37.     X *p;
  38.     a = (p+10)->f();
  39.  
  40. where (p+10) will survive through the call. In gcc-2.x I wrote a small string class which had
  41. a member function 'string sub(int pos, int len)' which results in a temporary. I could do
  42.  
  43.     s2 = s.sub(2,3);
  44.  
  45. but not
  46.  
  47.     s2 = s.sub(2,3).strip();
  48.  
  49. because the temporary representing 's.sub(2,3)' was destroyed before tmp.strip(). This is kind of
  50. confusing. 
  51.  
  52. So let temporaries live until end of statement.
  53.  
  54. > --
  55. > Prescott K. Turner, Jr.
  56. > 13 Burning Tree Rd., Natick, Massachusetts USA
  57. > Internet: pkturner@world.std.com
  58.  
  59.  
  60.  
  61. /jonas
  62.  
  63.