home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12907 < prev    next >
Encoding:
Text File  |  1992-08-26  |  5.7 KB  |  129 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!mundil.cs.mu.OZ.AU!fjh
  3. From: fjh@mundil.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Proposal: auto T&
  5. Message-ID: <9223922.28196@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <1992Aug19.234913.622@tfs.com> <2A991ABB.477D@tct.com> <78402@ut-emx.uucp>
  9. Date: Wed, 26 Aug 1992 12:40:01 GMT
  10. Lines: 117
  11.  
  12. jamshid@ut-emx.uucp (Jamshid Afshar) writes:
  13.  
  14. >In article <2A991ABB.477D@tct.com> chip@tct.com (Chip Salzenberg) writes:
  15. >>According to eric@tfs.com (Eric Smith):
  16. >>>The "auto" in the function return value type tells the compiler to allocate
  17. >>>the space for that automatic in the stack frame of the caller, instead of
  18. >>>the stack frame of the called function.
  19. >>
  20. >>There is prior art.  GCC and G++ already allow named return values ...
  21. >
  22. >But is there current reason?  Can named return values let compilers do
  23. >anything that they cannot do with a little bit of optimization (ARM 12
  24. >Commentary)?
  25.  
  26. Yes, named return values allows a function to construct the return
  27. value at the start of the function instead of just before actually
  28. returning it.
  29.  
  30. You can also use auxiliary inline functions in combination with named
  31. return values to effectively construct the return value at any point in 
  32. the middle of the function, but this is very messy.
  33.  
  34. The reason for wanting to be able to construct the return value anywhere
  35. in the function is to avoid having to introduce a temporary to hold
  36. the return value, since the additional copying and destruction can be
  37. quite innefficient, especially if the objects concerned contain dynamically
  38. allocated data.
  39.  
  40. I suggested that instead of introducing some new syntax for this, compilers
  41. should be allowed to optimize away the named temporaries. Barry Margolin
  42. pointed out that the ARM disallows that in most cases, and Thomas M.
  43. Breuel replied:
  44.  
  45. >barmar@think.com (Barry Margolin) writes:
  46. >
  47. >   fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
  48. >   >Here again compilers should be smart enough to optimize away the both the
  49. >   >unnamed temporary AND the named temporary "retval".
  50. >   >I don't know whether the latter optimization is allowed by the ARM
  51. >   >(I suspect not), but it SHOULD be.
  52. >
  53. >   It is in limited cases.  Sec. 3.5 of the ARM explicitly says, "nor may an
  54. >   automatic named object of a class with a constructor or a destructor with
  55. >   side effects be eliminated even if it appears to be unused."  In most
  56. >   cases, I suspect compilers won't check for the "with side effects" (if the
  57. >   constructor or destructor isn't inline, they generally can't), and some
  58. >   compilers may not even check whether the class has a contructor or
  59. >   destructor.
  60. >
  61. >Yes, and this rule is unlikely to change. People are using
  62. >constructors/destructors not only for memory management but also as a
  63. >form of UNWIND-PROTECT (e.g., create a window on block entry and make
  64. >sure it gets destroyed on block exit, no matter what).
  65.  
  66. Maybe I wasn't specific enough about the optimization I wanted to allow.
  67. The idea is that whenever an object is copied and then immediately 
  68. destroyed, it amounts to moving the object. If possible, it would be
  69. more efficient to construct the object at the right location in the first
  70. place. Thus when you see a sequence of instructions like
  71.  
  72.     construct an X at location A
  73.     ...
  74.     copy X from location A to B using the copy-constructor
  75.     destroy X at location A using destructor
  76.  
  77. they could be replaced by
  78.  
  79.     construct an X at location B
  80.     ...
  81.  
  82. I was not simply proposing that any variable that is not used apart
  83. from having it's constructor/destructor called should be eliminated.
  84. Code such as
  85.     { window w(...); ... /* w not used */ ... }
  86. would not be affected.  As you say, Thomas, that would prevent the
  87. style of using constructors and destructors deliberately for their side
  88. effects. On the contrary, I think that the optimization would SUPPORT
  89. this style of programming, if only one could ensure that all compilers
  90. applied it (an impossible task, to be sure :-).
  91.  
  92. For example, say I have code like the following:
  93.  
  94.     Window error_message_window(int x, int y, const char *message) {
  95.         Window the_window(x, y, "Error", ERROR_COLOURS);
  96.         the_window << message;
  97.         return the_window;
  98.     }
  99.  
  100. Now the problem is that the "return the_window" statement has 
  101. *undesireable* hidden effects, because it does not simply make the
  102. return value available to the calling function, instead it does all
  103. this copying and destruction behind the scenes, which causes my screen
  104. to be refreshed 3 times instead of once.
  105.  
  106. In this particular case one solution is to make error_message_window
  107. a derived class of window; however in general this may not be appropriate.
  108.  
  109. Perhaps my suggestion is not the right way to solve the problem, simply
  110. because you can never guarantee that compilers will always apply the
  111. optimization. However I don't think it would break anyone's code.
  112. (If anyone has got an example of real code that would break, please post
  113. it! If anyone thinks that it *would* break someones code, please
  114. explain why!). I don't think that the GNU extension is general enough.
  115. The advantage of my suggestion is that it does not require any 
  116. extensions to the language.
  117.  
  118. The philosophy of C and C++ has long been that if you don't use
  119. a feature, you shouldn't have to pay for it. If my code doesn't
  120. depend on side-effects when moving an object from one location to
  121. another during return statements, why should I have to pay the cost
  122. of all the unnecessary copying and destruction?
  123.  
  124. -- 
  125. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  126. This .signature virus is a self-referential statement that is true - but 
  127. you will only be able to consistently believe it if you copy it to your own
  128. .signature file!
  129.