home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13642 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  3.3 KB

  1. Path: sparky!uunet!wupost!swbatl!texbell!swuts!sw1sta!sg3235
  2. From: sg3235@sw1sta.uucp (Stephen Gevers)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Answers on a postcard...
  5. Message-ID: <1425@swuts.sbc.com>
  6. Date: 14 Sep 92 13:25:31 GMT
  7. References: <92254.104002KKEYTE@ESOC.BITNET> <1992Sep10.174914.838@taumet.com> <92255.112151KKEYTE@ESOC.BITNET>
  8. Sender: usenet@swuts.sbc.com
  9. Organization: Southwestern Bell
  10. Lines: 67
  11.  
  12. In article <92255.112151KKEYTE@ESOC.BITNET> KKEYTE@ESOC.BITNET (Karl Keyte) writes:
  13. ]In article <1992Sep10.174914.838@taumet.com>, steve@taumet.com (Steve Clamage)
  14. ]says:
  15. ]>
  16. ]>Karl Keyte <KKEYTE@ESOC.BITNET> writes:
  17. ]>
  18. ]>|#include <iostream.h>
  19. ]>
  20. ]>|class X {
  21. ]>|   private:    int           value;
  22. ]>|   public:     int           operator , (int i) { return i+10; };
  23. ]>|               int           operator + (int i) { return value+i; };
  24. ]>|               X &           operator = (int i) { value=i; return *this; };
  25. ]>|};
  26. ]>
  27. ]>
  28. ]>|void main()
  29. ]>|{
  30. ]>|   X         x;
  31. ]>
  32. ]>|   cout << "Value is " << (x=1, x+1) << endl;
  33. ]>|}
  34. ]>
  35. ]>In the expression in parens, it is unspecified whether "x=1" or
  36. ]>"x+1" is evaluated first.  They are arguments to the "operator,()"
  37. ]>function, not the left and right side of a comma-expression.  If
  38. ]>"x+1" is evaluated first, which is legal, it will use an
  39. ]>uninitialized "x.value".
  40. ]>
  41. ]>Don't write code which depends on unspecified order of evaluation.
  42. ]>
  43. ]>If there were no "op,()", the "," would mark a comma-expression,
  44. ]>and the "x=1" would have to be evaluated first; in that case the
  45. ]>whole expression would be well-defined.
  46. ]
  47. ]The implication of what you say is that it is not possible to overload
  48. ]operators maintaining the correct functionality of the operator.  This
  49. ]indeed means that precedence in overloaded operators cannot be maintained
  50. ]if all operands are simply passed as arguments to the function implementing
  51. ]the operator functionality and evaluated in a language independent order.
  52. ]Imagine commonly used "chain" operators such as && and ::.
  53. ]
  54. ]If what is described above is correct, and Mr.Clamage clearly has much
  55. ]experience in the area, then I think we have justification for pressing
  56. ]for a change in this area.  If not, I would ask: How does one overload
  57. ]the comma operator while maintaining its attibutes as an operator?
  58. ]
  59. ]Karl
  60.  
  61. While I understand Mr. Clamage's explanation, I do not see any support of it
  62. in the ARM.  Specifically, section 5.18 states "A pair of expressions separated
  63. by a comma is evaluated left-to-right and the value of the left expression is
  64. discarded.  All side effects of the left expression are performed before the 
  65. evaluation of the right expression."  Section 5 states "Operator overloading
  66. cannot modify the rules for operators applied to types for which they are
  67. defined by the language itself."  Finally, the commentary in section 13.4 says
  68. "In particular, overloaded operators , and ->* obey exactly the same rules as
  69. other overloaded binary operators, such as ^ and %."
  70.  
  71. I have the 1st edition of the ARM and I don't know if there is any more
  72. specific reference to the comma operator in the second edition.  I tested the
  73. above code on Borland C++ 3.0 and Cfront 2.0.  Borland gave a result in the
  74. high 6000's and Cfront gave the result of 12.  BTW, has anyone really used the
  75. comma operator for something useful???
  76.  
  77. Stephen Gevers
  78. sw1sta.sbc.com
  79.