home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / cplus / 2134 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.6 KB  |  65 lines

  1. Path: sparky!uunet!charon.amdahl.com!amdahl!rtech!sgiblab!sdd.hp.com!saimiri.primate.wisc.edu!ames!agate!forney.berkeley.edu!jbuck
  2. From: jbuck@forney.berkeley.edu (Joe Buck)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Question about temporaries
  5. Date: 27 Jan 1993 20:11:19 GMT
  6. Organization: U. C. Berkeley
  7. Lines: 53
  8. Message-ID: <1k6q97$c33@agate.berkeley.edu>
  9. References: <C1DtoB.FJ3@fiu.edu> <1993Jan25.163150.24449@lpi.liant.com>
  10. NNTP-Posting-Host: forney.berkeley.edu
  11.  
  12. In article <1993Jan25.163150.24449@lpi.liant.com> pkt@lpi.liant.com (Scott Turner) writes:
  13. > [proposed rule on lifetime of temporaries]
  14. >     Temporaries are normally destroyed at the end of the statement
  15. >     in which they are created.  The exception is that temporaries
  16. >     created within a branch of a conditional (?:) operator are destroyed
  17. >     when that branch is completed (and analogously for && and ||).
  18.  
  19. This seems like the most reasonable compromise.
  20.  
  21. >I would say that the committee was ready to compromise and decide on this
  22. >except for one thing.  Nearly all of the discussion has assumed that we
  23. >know what is a "temporary".  When asked directly regarding the formal
  24. >argument x in the function
  25.  
  26.     class C { ...  C (const C&);  ... };
  27.     void f(class C x) {   ... use x ...   }
  28.  
  29. >most people would say x is _not_ a temporary.
  30.  
  31. This is true, but that has nothing to do with your example below.  The
  32. formal parameter x is a different thing entirely from the actual
  33. parameter that is assigned to it.  The latter may be a temporary but
  34. the former is not.
  35.  
  36. I changed the variable from x to w in your example to avoid confusion
  37. (two different x's).
  38.  
  39.      String w, y;
  40.      char *z;
  41.      extern char *f(String x);
  42.      z = f(w+y);
  43.  
  44. In this example there is a temporary that is the result of w+y.  This
  45. temporary gets assigned to the formal parameter x of function f by means
  46. of the copy constructor, at least conceptually.  But it could still be
  47. considered a temporary in the sense of the rule above, meaning that it
  48. lives until the semicolon.  Under this interpretation, we would have the
  49. following action: temporary t is set to the result of w+y; this temporary
  50. is passed to the x parameter of f with the copy constructor; at the end
  51. of f that copy is destroyed, but t is still alive; the result is assigned
  52. to z, and then t is destroyed.
  53.  
  54. The result is the same as if we replace the final statement above with
  55.  
  56.     { String& t = w+y; z = f(t);}
  57.  
  58. Note that this has a cost: we cannot eliminate the copy constructor and
  59. initialize x "in place" anymore.  This means that the fundamental cost
  60. of passing an object by value has increased.
  61.  
  62.  
  63. --
  64. Joe Buck    jbuck@ohm.berkeley.edu
  65.