home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / cplus / 1545 < prev    next >
Encoding:
Text File  |  1992-11-11  |  2.5 KB  |  95 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!netsys!agate!ames!sun-barr!cs.utexas.edu!swrinde!sdd.hp.com!apollo.hp.com!cupnews0.cup.hp.com!scd.hp.com!hpscdm!hpscdc!vinoski
  3. From: vinoski@ch.apollo.hp.com (Stephen Vinoski)
  4. Subject: return values and destructors
  5. Message-ID: <BxKs87.417@scd.hp.com>
  6. Sender: news@scd.hp.com (News Account)
  7. Organization: Hewlett-Packard Corporation, Chelmsford, MA
  8. Date: Wed, 11 Nov 1992 23:36:07 GMT
  9. Lines: 84
  10.  
  11.  
  12. In section 6.6.3 on page 90 the ARM states:
  13.  
  14. "A return statement with an expression can be used only in functions
  15. returning a value; the value of the expression is returned to the
  16. caller of the function."
  17.  
  18. Given the statement
  19.  
  20.     return foo;
  21.  
  22. where foo is an object with some value, does the statement from the
  23. ARM mean that the compiler *must* return the value that foo has in the
  24. return statement, rather than the value foo might have at some point
  25. after the destructors for local automatic objects have been executed?
  26.  
  27. Consider this example:
  28.  
  29. template<class T>
  30. class A
  31. {
  32.   public:
  33.     A(T *p) : ptr(p) {}
  34.     ~A() {if (ptr) *ptr = 0;}
  35.   private:
  36.     T *ptr;
  37. };
  38.  
  39.  
  40. int
  41. func1()
  42. {
  43.     int x = 10;
  44.     A<int> a(&x);
  45.     return x;
  46. }
  47.  
  48.  
  49. Under cfront 3.0.1, func1() returns 0, not 10, even though the value of
  50. x in the return statement is clearly 10.  The value of x is changed to
  51. 0 due to the destructor of the A object going out of scope at the end
  52. of the function.
  53.  
  54. What's more interesting is that if a func2() is created that returns
  55. by value an object of a class type with a constructor and destructor,
  56. the desired value is returned because of the generation of a
  57. temporary:
  58.  
  59. class B
  60. {
  61.   public:
  62.     B(int x) : val(x) {}
  63.     ~B() {}
  64.     void operator=(int x) {val = x;}
  65.     operator int() {return val;}
  66.   private:
  67.     int val;
  68. };
  69.  
  70. B
  71. func2()
  72. {
  73.     B b(10);
  74.     A<B> a(&b);    // same A as above
  75.     return b;
  76. }
  77.  
  78.  
  79. In this case, func2() returns a B whose val member has a value of 10,
  80. due to the generation of a temporary, even though the same A
  81. destructor sets the value to 0.
  82.  
  83. Shouldn't both of these cases perform the same way?  Is this a cfront
  84. bug?  Shouldn't the value of the return expression be returned to the
  85. caller regardless of side effects of destructors for auto objects?
  86. Does the language enforce any of this?  The pre-Boston mailing doesn't
  87. seem to discuss this any differently than the ARM does.
  88.  
  89. thanks,
  90. -steve
  91. -- 
  92. Steve Vinoski  (508)436-5904   vinoski@apollo.hp.com
  93. Distributed Object Computing Program
  94. Hewlett-Packard, Chelmsford, MA 01824       These are my opinions.
  95.